-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek11b.java
More file actions
35 lines (35 loc) · 1.1 KB
/
week11b.java
File metadata and controls
35 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class week11b {
public static void main(String[] args) {
Reserve r=new Reserve(1);
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.setName("First passenger");
t2.setName("Second passenger");
t1.start();
t2.start();
}
static class Reserve implements Runnable{
int available=2;
int wanted;
Reserve(int wanted){
this.wanted=wanted;
}
public void run() {
synchronized (this){
System.out.println("Available seats="+available);
if(available>=wanted){
String name=Thread.currentThread().getName();
System.out.println(wanted+"berths reserved for"+name);
try{
Thread.sleep(1500);
available-=wanted;
}catch (InterruptedException ie){
ie.printStackTrace();
}
}else{
System.out.println("Sorry no berths");
}
}
}
}
}