-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.java
75 lines (75 loc) · 971 Bytes
/
A.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class A implements Runnable
{
int n=-1;
int a[]=new int[15];
Thread t;
boolean v=false;
A(String s)
{
t=new Thread(this,s);
t.start();
}
public void pop()
{
for(;n!=-1;n--)
{
if(!v&&n==-1)
{
v=true;
notify();
}
if(n==-1)
{
try{
wait();
}catch(Exception e){System.out.println(e);}
notify();
}
System.out.println(a[n]);
}
}
public void push(int d)
{
for(n=0;n<15;)
{
if(v&&n==14)
{
v=false;
notify();
}
if(n==14)
{
try{
wait();
}catch(Exception e){System.out.println(e);}
}
n++;
a[n]=++d;
System.out.println(a[n]);
}
}
public void dis()
{
for(int i=0;i<a.length-1;i++)
{
System.out.println(a[i]);
}
}
public void run()
{
if(Thread.currentThread().getName().equals("SI"))
{
this.push(10);
}
else
this.pop();
}
public static void main(String s[])
{
new A("SD");
new A("SI");
//Thread t1=new Thread("SID");
//t.start();
//t1.start();
}
}