-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion57.java
More file actions
45 lines (41 loc) · 734 Bytes
/
Copy pathQuestion57.java
File metadata and controls
45 lines (41 loc) · 734 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
// program to demonstrate use of synchronization
class Demo {
synchronized void print (int n) {
for (int i = 1; i <= 3; i++) {
System.out.println (n * i);
try {
Thread.sleep (500);
}
catch (Exception e) {
System.out.println (e);
}
}
}
}
class MyThread1 extends Thread {
Demo d;
MyThread1 (Demo d) {
this.d = d;
}
public void run () {
d.print (5);
}
}
class MyThread2 extends Thread {
Demo d;
MyThread2 (Demo d) {
this.d = d;
}
public void run () {
d.print (10);
}
}
public class Question57 {
public static void main (String[] args) {
Demo obj = new Demo ();
MyThread1 obj1 = new MyThread1 (obj);
MyThread2 obj2 = new MyThread2 (obj);
obj1.start ();
obj2.start ();
}
}