Skip to content

Commit ad88db4

Browse files
committed
LockSupportDemo
1 parent 85aff43 commit ad88db4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/chapter3/LockSupportDemo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chapter3;
2+
3+
import java.util.concurrent.locks.LockSupport;
4+
5+
/**
6+
* Created by 13 on 2017/5/5.
7+
*/
8+
public class LockSupportDemo {
9+
public static Object u = new Object();
10+
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
11+
static ChangeObjectThread t2 = new ChangeObjectThread("t2");
12+
13+
public static class ChangeObjectThread extends Thread {
14+
public ChangeObjectThread(String name) {
15+
super.setName(name);
16+
}
17+
18+
public void run() {
19+
synchronized (u) {
20+
System.out.println("in " + getName());
21+
LockSupport.park();
22+
}
23+
}
24+
}
25+
26+
27+
public static void main(String args[]) throws InterruptedException {
28+
t1.start();
29+
Thread.sleep(100);
30+
t2.start();
31+
LockSupport.unpark(t1);
32+
LockSupport.unpark(t2);
33+
t1.join();
34+
t2.join();
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package chapter3;
2+
3+
import java.util.concurrent.locks.LockSupport;
4+
5+
/**
6+
* Created by 13 on 2017/5/5.
7+
*/
8+
public class LockSupportIntDemo {
9+
public static Object u = new Object();
10+
static ChangeObjectThread t1 = new ChangeObjectThread("t1");
11+
static ChangeObjectThread t2 = new ChangeObjectThread("t2");
12+
13+
public static class ChangeObjectThread extends Thread {
14+
public ChangeObjectThread(String name) {
15+
super.setName(name);
16+
}
17+
18+
public void run() {
19+
synchronized (u) {
20+
System.out.println("in " + getName());
21+
LockSupport.park();
22+
if (Thread.interrupted()) {
23+
}
24+
System.out.println(getName() + "±»ÖжÏÁË");
25+
}
26+
System.out.println(getName() + "Ö´ÐнáÊø");
27+
}
28+
}
29+
30+
31+
public static void main(String args[]) throws InterruptedException {
32+
t1.start();
33+
Thread.sleep(100);
34+
t2.start();
35+
LockSupport.unpark(t1);
36+
LockSupport.unpark(t2);
37+
t1.join();
38+
t2.join();
39+
}
40+
41+
}

0 commit comments

Comments
 (0)