File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments