-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinAndSynchExample.java
More file actions
74 lines (66 loc) · 1.87 KB
/
JoinAndSynchExample.java
File metadata and controls
74 lines (66 loc) · 1.87 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
package Threads;
class MyLogic {
static int x = 1;
static void print() {
for (int i = 1; i <= 5; i++, x++) {
/*
* Thread.currentThread().isAlive(); Thread.currentThread().stop();
* Thread.currentThread().suspend();
* Thread.currentThread().resume();
*/
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Thread.yield();
// Thread.currentThread().setDaemon(true);
System.out
.println("Current Thread name " + "" + Thread.currentThread().getName() + " I is " + i + " X " + x);
}
}
}
class Job2 implements Runnable {
int x = 100;
MyLogic object = new MyLogic();
@Override
// public synchronized void run(){
public void run() {
// System.out.println("Not Synch "+Thread.currentThread().getName());
synchronized (MyLogic.class) {
MyLogic.print();
}
// synchronized (object) {
// object.print();
// }
/*
* System.out.println("this is not synchronized ..."+Thread.
* currentThread());
* System.out.println("this is not synchronized ..."+Thread.
* currentThread());
* System.out.println("this is not synchronized ..."+Thread.
* currentThread()); synchronized (this) {
*
* for(int i = 1 ; i<=5 ; i++,x++){
* System.out.println("Current Thread name " +
* ""+Thread.currentThread().getName()+" I is "+i+" X "+x); } }
*/
// System.out.println("Not Synch2 "+Thread.currentThread().getName());
}
}
public class JoinAndSynchExample {
public static void main(String[] args) throws InterruptedException {
Job2 job = new Job2();
Thread worker1 = new Thread(job, "worker1");
Thread worker2 = new Thread(job, "worker2");
Thread worker3 = new Thread(job, "worker3");
// worker1.isAlive();
worker1.start();
// worker1.setDaemon(true);
// worker1.sleep(1000);
// worker1.join();
worker2.start();
worker3.start();
}
}