Skip to content

Commit 68af61f

Browse files
committed
update
1 parent f3f6632 commit 68af61f

File tree

7 files changed

+66
-18
lines changed

7 files changed

+66
-18
lines changed

03concurrency/0301/src/main/java/java0/conc0302/atomic/AtomicMain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public class AtomicMain {
55

66
public static void main(String[] args) {
7-
final AtomicCount count = new AtomicCount();
7+
final SyncCount count = new SyncCount();
88
for (int i = 0; i < 100; i++) {
99
new Thread(new Runnable() {
1010
@Override
@@ -17,7 +17,7 @@ public void run() {
1717
}
1818

1919
try {
20-
Thread.sleep(1000L);
20+
Thread.sleep(5000L);
2121
} catch (InterruptedException e) {
2222
e.printStackTrace();
2323
}

03concurrency/0301/src/main/java/java0/conc0302/atomic/SyncCount.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11

22
package java0.conc0302.atomic;
33

4+
import java.util.concurrent.locks.Lock;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
47
public class SyncCount {
58

69
private int num = 0;
710

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+
}
1020
}
1121

1222
public int getNum() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+

03concurrency/0301/src/main/java/java0/conc0302/threadpool/ExceptionDemo.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ public static void main(String[] args) {
2121
System.out.println("catch submit");
2222
ex.printStackTrace();
2323
}
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+
//
3434
executorService.shutdown();
3535
System.out.println("Main Thread End!");
3636
}

03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class CountDownLatchDemo {
66
public static void main(String[] args) throws InterruptedException {
77
CountDownLatch countDownLatch = new CountDownLatch(5);
8-
for(int i=0;i<10;i++){
8+
for(int i=0;i<5;i++){
99
new Thread(new readNum(i,countDownLatch)).start();
1010
}
1111
countDownLatch.await(); // 注意跟CyclicBarrier不同,这里在主线程await

03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public void run() {
3939
synchronized (this){
4040
System.out.println("id:"+id+","+Thread.currentThread().getName());
4141
try {
42-
//cyc.await();
42+
cyc.await();
4343
System.out.println("线程组任务" + id + "结束,其他任务继续");
44-
cyc.await(); // 注意跟CountDownLatch不同,这里在子线程await
44+
//cyc.await(); // 注意跟CountDownLatch不同,这里在子线程await
4545
} catch (Exception e) {
4646
e.printStackTrace();
4747
}

03concurrency/0301/src/main/java/java0/conc0303/tool/SemaphoreDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SemaphoreDemo {
66

77
public static void main(String[] args) {
88
int N = 8; //工人数
9-
Semaphore semaphore = new Semaphore(1); //机器数目
9+
Semaphore semaphore = new Semaphore(2); //机器数目
1010
for (int i = 0; i < N; i++)
1111
new Worker(i, semaphore).start();
1212
}

0 commit comments

Comments
 (0)