File tree Expand file tree Collapse file tree 6 files changed +228
-0
lines changed
java/com/javaedge/concurrency/example Expand file tree Collapse file tree 6 files changed +228
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class LockSupportBlockerTest {
2
+ public static void main (String [] args ) {
3
+ Thread t3 = new Thread (() -> {
4
+ Thread .currentThread ().setName ("t3" );
5
+ System .out .println (Thread .currentThread ().getName () + " park 5 seconds" );
6
+ //park 5 seconds, set blocker
7
+ Object blocker = new String ("sss" );
8
+ LockSupport .parkUntil (blocker , System .currentTimeMillis () + TimeUnit .MILLISECONDS .convert (5 , TimeUnit .SECONDS ));
9
+ System .out .println (Thread .currentThread ().getName () + " after park" );
10
+ });
11
+ t3 .start ();
12
+
13
+ try {
14
+ Object t3_blocker = null ;
15
+ while (t3_blocker == null ) {
16
+ t3_blocker = LockSupport .getBlocker (t3 );
17
+ TimeUnit .MILLISECONDS .sleep (10 );
18
+ }
19
+ System .out .println ("t3 blocker is :" + t3_blocker );
20
+ t3 .join ();
21
+ } catch (InterruptedException e ) {
22
+ e .printStackTrace ();
23
+ }
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ public class LockSupportExam {
2
+ public static void main (String [] args ) {
3
+ Thread t1 = new Thread () {
4
+ @ Override
5
+ public void run () {
6
+ Thread .currentThread ().setName ("t1" );
7
+ System .out .println (Thread .currentThread ().getName () + " before park" );
8
+ //park 100 seconds
9
+ LockSupport .parkNanos (TimeUnit .NANOSECONDS .convert (100 , TimeUnit .SECONDS ));
10
+ System .out .println (Thread .currentThread ().getName () + " after park" );
11
+ }
12
+ };
13
+ Thread t2 = new Thread () {
14
+ @ Override
15
+ public void run () {
16
+ try {
17
+ Thread .currentThread ().setName ("t2" );
18
+ TimeUnit .SECONDS .sleep (1 );
19
+ System .out .println (Thread .currentThread ().getName () + " unpark t1" );
20
+ LockSupport .unpark (t1 );
21
+ } catch (InterruptedException e ) {
22
+ e .printStackTrace ();
23
+ }
24
+
25
+ }
26
+ };
27
+ Thread t3 = new Thread () {
28
+ @ Override
29
+ public void run () {
30
+ Thread .currentThread ().setName ("t3" );
31
+ System .out .println (Thread .currentThread ().getName () + " park 5 seconds" );
32
+ //park 5 seconds
33
+ LockSupport .parkUntil (System .currentTimeMillis () + TimeUnit .MILLISECONDS .convert (5 ,TimeUnit .SECONDS ));
34
+ System .out .println (Thread .currentThread ().getName () + " after park" );
35
+ }
36
+ };
37
+ t1 .start ();
38
+ t2 .start ();
39
+ t3 .start ();
40
+ try {
41
+ t1 .join ();
42
+ t2 .join ();
43
+ t3 .join ();
44
+ } catch (InterruptedException e ) {
45
+ e .printStackTrace ();
46
+ }
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ public class UnsafeTest {
2
+
3
+ public static void main (String [] args ) throws NoSuchFieldException , IllegalAccessException {
4
+ Field f = Unsafe .class .getDeclaredField ("theUnsafe" );
5
+ f .setAccessible (true );
6
+ Unsafe unsafe = (Unsafe ) f .get (null );
7
+ Thread t1 = new Thread () {
8
+ @ Override
9
+ public void run () {
10
+ Thread .currentThread ().setName ("t1" );
11
+ System .out .println (Thread .currentThread ().getName () + " before park" );
12
+ //park 100 seconds
13
+ unsafe .park (false , TimeUnit .NANOSECONDS .convert (100 , TimeUnit .SECONDS ));
14
+ System .out .println (Thread .currentThread ().getName () + " after park" );
15
+ }
16
+ };
17
+ Thread t2 = new Thread () {
18
+ @ Override
19
+ public void run () {
20
+ try {
21
+ Thread .currentThread ().setName ("t2" );
22
+ TimeUnit .SECONDS .sleep (1 );
23
+ System .out .println (Thread .currentThread ().getName () + " unpark t1" );
24
+ unsafe .unpark (t1 );
25
+ } catch (InterruptedException e ) {
26
+ e .printStackTrace ();
27
+ }
28
+
29
+ }
30
+ };
31
+ Thread t3 = new Thread () {
32
+ @ Override
33
+ public void run () {
34
+ Thread .currentThread ().setName ("t3" );
35
+ System .out .println (Thread .currentThread ().getName () + " park 5 seconds" );
36
+ //park 5 seconds
37
+ unsafe .park (true , System .currentTimeMillis () + TimeUnit .MILLISECONDS .convert (5 ,TimeUnit .SECONDS ));
38
+ System .out .println (Thread .currentThread ().getName () + " after park" );
39
+ }
40
+ };
41
+ t1 .start ();
42
+ t2 .start ();
43
+ t3 .start ();
44
+ try {
45
+ t1 .join ();
46
+ t2 .join ();
47
+ t3 .join ();
48
+ } catch (InterruptedException e ) {
49
+ e .printStackTrace ();
50
+ }
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ public class SimpleLock {
2
+ private static class Sync extends AbstractQueuedSynchronizer {
3
+ @ Override
4
+ protected boolean tryAcquire (int ignore ) {
5
+ return compareAndSetState (0 , 1 );
6
+ }
7
+
8
+ @ Override
9
+ protected boolean tryRelease (int ignore ) {
10
+ setState (0 );
11
+ return true ;
12
+ }
13
+
14
+ protected Sync () {
15
+ super ();
16
+ }
17
+ }
18
+
19
+ private final Sync sync = new Sync ();
20
+
21
+ public void lock () {
22
+ sync .acquire (1 );
23
+ }
24
+
25
+ public void unlock () {
26
+ sync .release (1 );
27
+ }
28
+
29
+ private static class MyThread extends Thread {
30
+ private final String name ;
31
+ private final SimpleLock lock ;
32
+
33
+ private MyThread (String name , SimpleLock lock ) {
34
+ this .name = name ;
35
+ this .lock = lock ;
36
+ }
37
+
38
+ @ Override
39
+ public void run () {
40
+ try {
41
+ lock .lock ();
42
+ System .out .println (name + " get the lock" );
43
+ TimeUnit .SECONDS .sleep (2 );
44
+ } catch (InterruptedException e ) {
45
+ e .printStackTrace ();
46
+ } finally {
47
+ lock .unlock ();
48
+ System .out .println (name + " release the lock" );
49
+ }
50
+ }
51
+ }
52
+
53
+ public static void main (String [] args ) {
54
+ final SimpleLock mutex = new SimpleLock ();
55
+ MyThread t1 = new MyThread ("t1" , mutex );
56
+ MyThread t2 = new MyThread ("t2" , mutex );
57
+ MyThread t3 = new MyThread ("t3" , mutex );
58
+ t1 .start ();
59
+ t2 .start ();
60
+ t3 .start ();
61
+ try {
62
+ t1 .join ();
63
+ t2 .join ();
64
+ t3 .join ();
65
+ } catch (InterruptedException e ) {
66
+ e .printStackTrace ();
67
+ }
68
+ System .out .println ("main thread exit!" );
69
+ }
70
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .concurrent .atomic .AtomicInteger ;
2
+
3
+ public class TicketLock {
4
+ private AtomicInteger serviceNum = new AtomicInteger (); // 服务号
5
+ private AtomicInteger ticketNum = new AtomicInteger (); // 排队号
6
+
7
+ public int lock () {
8
+ // 首先原子性地获得一个排队号
9
+ int myTicketNum = ticketNum .getAndIncrement ();
10
+
11
+ // 只要当前服务号不是自己的就不断轮询
12
+ while (serviceNum .get () != myTicketNum ) {
13
+ }
14
+
15
+ return myTicketNum ;
16
+ }
17
+
18
+ public void unlock (int myTicket ) {
19
+ // 只有当前线程拥有者才能释放锁
20
+ int next = myTicket + 1 ;
21
+ serviceNum .compareAndSet (myTicket , next );
22
+ }
23
+ }
Original file line number Diff line number Diff line change 7
7
</content >
8
8
<orderEntry type =" inheritedJdk" />
9
9
<orderEntry type =" sourceFolder" forTests =" false" />
10
+ <orderEntry type =" module-library" >
11
+ <library name =" JUnit4" >
12
+ <CLASSES >
13
+ <root url =" jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14
+ <root url =" jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15
+ </CLASSES >
16
+ <JAVADOC />
17
+ <SOURCES />
18
+ </library >
19
+ </orderEntry >
10
20
</component >
11
21
</module >
You can’t perform that action at this time.
0 commit comments