-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSynchronizedDemo2.java
More file actions
62 lines (58 loc) · 992 Bytes
/
SynchronizedDemo2.java
File metadata and controls
62 lines (58 loc) · 992 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Display
{
public synchronized void displayn()
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e){}
}
}
public synchronized void displayc()
{
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.displayn();}
}
class MyThread2 extends Thread
{
Display d;
MyThread2(Display d)
{
this.d=d;
}
public void run()
{d.displayc();}
}
public class SynchronizedDemo2
{
public static void main(String[] args)
{
Display d=new Display();
MyThread1 thread1=new MyThread1(d);
MyThread2 thread2=new MyThread2(d);
thread1.start();
thread2.start();
}
}