File tree Expand file tree Collapse file tree 7 files changed +192
-0
lines changed
03concurrency/0301/src/main/java/java0/conc0302 Expand file tree Collapse file tree 7 files changed +192
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .atomic ;
3+
4+ import java .util .concurrent .atomic .AtomicInteger ;
5+
6+ public class AtomicCount {
7+
8+ private AtomicInteger num = new AtomicInteger ();
9+
10+ public synchronized int add () {
11+ return num .getAndIncrement ();
12+ }
13+
14+ public int getNum () {
15+ return num .get ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .atomic ;
3+
4+ public class AtomicMain {
5+
6+ public static void main (String [] args ) {
7+ final AtomicCount count = new AtomicCount ();
8+ for (int i = 0 ; i < 100 ; i ++) {
9+ new Thread (new Runnable () {
10+ @ Override
11+ public void run () {
12+ for (int j = 0 ; j < 10000 ; j ++) {
13+ count .add ();
14+ }
15+ }
16+ }).start ();
17+ }
18+
19+ try {
20+ Thread .sleep (1000L );
21+ } catch (InterruptedException e ) {
22+ e .printStackTrace ();
23+ }
24+
25+ System .out .println ("num=" + count .getNum ());
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .atomic ;
3+
4+ public class Count {
5+
6+ private int num = 0 ;
7+
8+ public int add () {
9+ return num ++;
10+ }
11+
12+ public int getNum () {
13+ return num ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0302 .atomic ;
2+
3+ import java .util .concurrent .atomic .AtomicLong ;
4+ import java .util .concurrent .atomic .LongAdder ;
5+
6+ public class LongDemo {
7+
8+ public static void main (String [] args ) {
9+
10+ final AtomicLong atomicLong = new AtomicLong ();
11+ final LongAdder longAdder = new LongAdder ();
12+
13+ for (int i = 0 ; i < 100 ; i ++) {
14+ new Thread (new Runnable () {
15+ @ Override
16+ public void run () {
17+ for (int j = 0 ; j < 10000 ; j ++) {
18+ atomicLong .getAndIncrement ();
19+ longAdder .increment ();
20+ }
21+ }
22+ }).start ();
23+ }
24+
25+ try {
26+ Thread .sleep (1000L );
27+ } catch (InterruptedException e ) {
28+ e .printStackTrace ();
29+ }
30+
31+ System .out .println ("atomicLong=" + atomicLong .get ());
32+ System .out .println ("longAdder =" + longAdder .sum ());
33+
34+ }
35+ }
Original file line number Diff line number Diff line change 1+
2+ package java0 .conc0302 .atomic ;
3+
4+ public class SyncCount {
5+
6+ private int num = 0 ;
7+
8+ public synchronized int add () {
9+ return num ++;
10+ }
11+
12+ public int getNum () {
13+ return num ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0302 .lock ;
2+
3+ import java .util .concurrent .locks .Condition ;
4+ import java .util .concurrent .locks .Lock ;
5+ import java .util .concurrent .locks .ReentrantLock ;
6+
7+ class ConditionDemo {
8+ final Lock lock = new ReentrantLock ();
9+ final Condition notFull = lock .newCondition ();
10+ final Condition notEmpty = lock .newCondition ();
11+
12+ final Object [] items = new Object [20 ];
13+ int putptr , takeptr , count ;
14+
15+ public void put (Object x ) throws InterruptedException {
16+ lock .lock ();
17+ try {
18+ // 当count等于数组的大小时,当前线程等待,直到notFull通知,再进行生产
19+ while (count == items .length )
20+ notFull .await ();
21+ items [putptr ] = x ;
22+ if (++putptr == items .length ) putptr = 0 ;
23+ ++count ;
24+ notEmpty .signal ();
25+ } finally {
26+ lock .unlock ();
27+ }
28+ }
29+
30+ public Object take () throws InterruptedException {
31+ lock .lock ();
32+ try {
33+ // 当count为0,进入等待,直到notEmpty通知,进行消费。
34+ while (count == 0 )
35+ notEmpty .await ();
36+ Object x = items [takeptr ];
37+ if (++takeptr == items .length ) takeptr = 0 ;
38+ --count ;
39+ notFull .signal ();
40+ return x ;
41+ } finally {
42+ lock .unlock ();
43+ }
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ package java0 .conc0302 .lock ;
2+
3+ import java .util .concurrent .locks .LockSupport ;
4+
5+ public class LockSupportDemo {
6+
7+ public static Object u = new Object ();
8+ static ChangeObjectThread t1 = new ChangeObjectThread ("t1" );
9+ static ChangeObjectThread t2 = new ChangeObjectThread ("t2" );
10+
11+ public static class ChangeObjectThread extends Thread {
12+ public ChangeObjectThread (String name ) {
13+ super (name );
14+ }
15+ @ Override public void run () {
16+ synchronized (u ) {
17+ System .out .println ("in " + getName ());
18+ LockSupport .park ();
19+ if (Thread .currentThread ().isInterrupted ()) {
20+ System .out .println ("被中断了" );
21+ }
22+ System .out .println ("继续执行" );
23+ }
24+ }
25+ }
26+
27+ public static void main (String [] args ) throws InterruptedException {
28+ t1 .start ();
29+ Thread .sleep (1000L );
30+ t2 .start ();
31+ Thread .sleep (3000L );
32+ t1 .interrupt ();
33+ LockSupport .unpark (t2 );
34+ t1 .join ();
35+ t2 .join ();
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments