- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo.java
More file actions
Latest commit
42 lines (38 loc) · 981 Bytes
/
Copy pathThreadDemo.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
classDisplay {
synchronizedvoidshow(inti) {
try {
System.out.println(i);
Thread.sleep(1500);
System.out.println(i);
} catch (InterruptedExceptione) {
System.out.println("Caught Exception");
}
}
}
classTimplementsRunnable {
Displayd = newDisplay();
T() {
Threadt1 = newThread(this);
t1.setName("odd");
Threadt2 = newThread(this);
t2.setName("even");
t1.start();
t2.start();
}
publicvoidrun() {
if (Thread.currentThread().getName().equals("odd")) {
for (inti = 1; i < 20; i = i + 2) {
this.d.show(i);
}
} else {
for (inti = 0; i < 20; i = i + 2) {
this.d.show(i);
}
}
}
}
classThreadDemo {
publicstaticvoidmain(Stringargs[]) {
Tobj = newT();
}
}