File tree Expand file tree Collapse file tree 7 files changed +66
-18
lines changed
03concurrency/0301/src/main/java/java0 Expand file tree Collapse file tree 7 files changed +66
-18
lines changed Original file line number Diff line number Diff line change 4
4
public class AtomicMain {
5
5
6
6
public static void main (String [] args ) {
7
- final AtomicCount count = new AtomicCount ();
7
+ final SyncCount count = new SyncCount ();
8
8
for (int i = 0 ; i < 100 ; i ++) {
9
9
new Thread (new Runnable () {
10
10
@ Override
@@ -17,7 +17,7 @@ public void run() {
17
17
}
18
18
19
19
try {
20
- Thread .sleep (1000L );
20
+ Thread .sleep (5000L );
21
21
} catch (InterruptedException e ) {
22
22
e .printStackTrace ();
23
23
}
Original file line number Diff line number Diff line change 1
1
2
2
package java0 .conc0302 .atomic ;
3
3
4
+ import java .util .concurrent .locks .Lock ;
5
+ import java .util .concurrent .locks .ReentrantLock ;
6
+
4
7
public class SyncCount {
5
8
6
9
private int num = 0 ;
7
10
8
- public synchronized int add () {
9
- return num ++;
11
+ private Lock lock = new ReentrantLock (true );
12
+
13
+ public int add () {
14
+ try {
15
+ lock .lock ();
16
+ return num ++;
17
+ } finally {
18
+ lock .unlock ();
19
+ }
10
20
}
11
21
12
22
public int getNum () {
Original file line number Diff line number Diff line change
1
+ package java0 .conc0302 .lock ;
2
+
3
+ import java .util .concurrent .atomic .AtomicReference ;
4
+ import java .util .concurrent .locks .ReentrantLock ;
5
+
6
+ public class TestFair {
7
+ public static volatile int race =0 ;
8
+ public static ReentrantLock lock = new ReentrantLock (true ); // 改成false会好100倍
9
+ public static void increase (){
10
+ lock .lock ();
11
+ race ++; //变量自增操作
12
+ lock .unlock ();
13
+ }
14
+ private static final int THREADS_COUNT =20 ;
15
+ public static void main (String []args ){
16
+ int count = Thread .activeCount ();
17
+ long now = System .currentTimeMillis ();
18
+ System .out .println (count );
19
+ AtomicReference <Thread > sign =new AtomicReference <>();
20
+ Thread []threads =new Thread [THREADS_COUNT ]; //定义20个线程
21
+ for (int i =0 ;i <THREADS_COUNT ;i ++){
22
+ threads [i ]=new Thread (new Runnable (){
23
+ @ Override
24
+ public void run (){
25
+ for (int i =0 ;i <100000 ;i ++){
26
+ increase ();
27
+ }
28
+ }
29
+ });
30
+ threads [i ].start ();
31
+ }//等待所有累加线程都结束
32
+ while (Thread .activeCount ()>count ) {
33
+ Thread .yield ();
34
+ }
35
+ System .out .println (lock .getClass ().getName () + " ts = " + (System .currentTimeMillis ()-now ));
36
+ }
37
+ }
38
+
Original file line number Diff line number Diff line change @@ -21,16 +21,16 @@ public static void main(String[] args) {
21
21
System .out .println ("catch submit" );
22
22
ex .printStackTrace ();
23
23
}
24
-
25
- try {
26
- executorService .execute (() -> {
27
- throw new RuntimeException ("executorService.execute()" );
28
- });
29
- } catch (Exception ex ) {
30
- System .out .println ("catch execute" );
31
- ex .printStackTrace ();
32
- }
33
-
24
+ //
25
+ // try {
26
+ // executorService.execute(() -> {
27
+ // throw new RuntimeException("executorService.execute()");
28
+ // });
29
+ // } catch (Exception ex) {
30
+ // System.out.println("catch execute");
31
+ // ex.printStackTrace();
32
+ // }
33
+ //
34
34
executorService .shutdown ();
35
35
System .out .println ("Main Thread End!" );
36
36
}
Original file line number Diff line number Diff line change 5
5
public class CountDownLatchDemo {
6
6
public static void main (String [] args ) throws InterruptedException {
7
7
CountDownLatch countDownLatch = new CountDownLatch (5 );
8
- for (int i =0 ;i <10 ;i ++){
8
+ for (int i =0 ;i <5 ;i ++){
9
9
new Thread (new readNum (i ,countDownLatch )).start ();
10
10
}
11
11
countDownLatch .await (); // 注意跟CyclicBarrier不同,这里在主线程await
Original file line number Diff line number Diff line change @@ -39,9 +39,9 @@ public void run() {
39
39
synchronized (this ){
40
40
System .out .println ("id:" +id +"," +Thread .currentThread ().getName ());
41
41
try {
42
- // cyc.await();
42
+ cyc .await ();
43
43
System .out .println ("线程组任务" + id + "结束,其他任务继续" );
44
- cyc .await (); // 注意跟CountDownLatch不同,这里在子线程await
44
+ // cyc.await(); // 注意跟CountDownLatch不同,这里在子线程await
45
45
} catch (Exception e ) {
46
46
e .printStackTrace ();
47
47
}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ public class SemaphoreDemo {
6
6
7
7
public static void main (String [] args ) {
8
8
int N = 8 ; //工人数
9
- Semaphore semaphore = new Semaphore (1 ); //机器数目
9
+ Semaphore semaphore = new Semaphore (2 ); //机器数目
10
10
for (int i = 0 ; i < N ; i ++)
11
11
new Worker (i , semaphore ).start ();
12
12
}
You can’t perform that action at this time.
0 commit comments