-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo.java
More file actions
42 lines (38 loc) · 981 Bytes
/
ThreadDemo.java
File metadata and controls
42 lines (38 loc) · 981 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
class Display {
synchronized void show(int i) {
try {
System.out.println(i);
Thread.sleep(1500);
System.out.println(i);
} catch (InterruptedException e) {
System.out.println("Caught Exception");
}
}
}
class T implements Runnable {
Display d = new Display();
T() {
Thread t1 = new Thread(this);
t1.setName("odd");
Thread t2 = new Thread(this);
t2.setName("even");
t1.start();
t2.start();
}
public void run() {
if (Thread.currentThread().getName().equals("odd")) {
for (int i = 1; i < 20; i = i + 2) {
this.d.show(i);
}
} else {
for (int i = 0; i < 20; i = i + 2) {
this.d.show(i);
}
}
}
}
class ThreadDemo {
public static void main(String args[]) {
T obj = new T();
}
}