-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncDemo99.java
79 lines (64 loc) · 893 Bytes
/
SyncDemo99.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
76
77
78
79
class Display
{
public synchronized void displaynum()
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{}
}
}
public synchronized void displaychar()
{
for(int i=65;i<=75;i++)
{
System.out.println((char)i);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{}
}
}
}
class MyThread1 extends Thread
{
Display d;
MyThread1(Display d)
{
this.d=d;
}
public void run()
{
d.displaynum();
}
}
class MyThread2 extends Thread
{
Display d;
MyThread2(Display d)
{
this.d=d;
}
public void run()
{
d.displaychar();
}
}
class SyncDemo99
{
public static void main(String[] args)
{
Display d = new Display();
MyThread1 t1=new MyThread1(d);
MyThread2 t2=new MyThread2(d);
t1.start();
t2.start();
}
}