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