-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedDemo2.java
More file actions
61 lines (51 loc) · 879 Bytes
/
Copy pathSynchronizedDemo2.java
File metadata and controls
61 lines (51 loc) · 879 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
class Display {
public synchronized void displayN(){
for (int i=0;i<5 ;i++ ) {
System.out.print(i);
try{ Thread.sleep(2000);
}
catch(InterruptedException e){
System.out.println(e);
}
}
}
public synchronized void displayC(){
for (int i=65;i<=75 ;i++ ) {
System.out.print((char)i);
try{
Thread.sleep(2000);
}
catch(InterruptedException e){
System.out.println(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();
}
}
class SynchronizedDemo2{
public static void main(String[] args){
Display d
= new Display();
MyThread1 t1 = new MyThread1(d);
MyThread2 t2 = new MyThread2(d);
t1.start();
t2.start();
}
}