forked from 20ayan/Hacktoberfest22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab10q9.java
More file actions
40 lines (40 loc) · 799 Bytes
/
Lab10q9.java
File metadata and controls
40 lines (40 loc) · 799 Bytes
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
36
37
38
39
40
class Reserve implements Runnable
{
int available=1;
int wanted;
Reserve(int i)
{
wanted=1;
}
public void run()
{
synchronized(this)
{
System.out.println("Available berths are:= "+available);
if(available>=wanted)
{
String name=Thread.currentThread().getName();
System.out.println(wanted+" Berth reserved for "+name);
try{
Thread.sleep(0);
available=available-wanted;
}
catch(InterruptedException ie){
}
}
else
System.out.println("Sorry no berths");
}
}
}
class Lab10q9{
public static void main(String args[]){
Reserve obj=new Reserve(1);
Thread t1=new Thread(obj);
Thread t2=new Thread(obj);
t1.setName("FIRST PERSON");
t2.setName("SECOND NAME");
t2.start();
t1.start();
}
}