一、使用Synchronized 同步代码块 ;
package thread.worker;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by hl on 2017/8/17.
*/
public class CountWorker {
private int unsafeSum = 0;
private synchronized void increase(CountWorker countWorker) {
countWorker.unsafeSum++;
}
@Test
public void testWorker() {
for (int j = 0; j < 10; j++) {
CountWorker countWorker = new CountWorker();
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
increase(countWorker);
}
}.start();
}
while (Thread.activeCount() > 1)
// 保证前面的线程都执行完
Thread.yield();
System.out.println(countWorker.unsafeSum);
}
}
}
10次计算,每次消耗的时间 :
elapse time is : 1058
elapse time is : 1002
elapse time is : 1002
elapse time is : 1002
elapse time is : 1001
elapse time is : 1002
elapse time is : 1001
elapse time is : 1002
elapse time is : 1002
elapse time is : 1001
二、使用 AtomicInteger 线程安全变量进行数据累加求和
package thread.worker;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by hl on 2017/8/17.
*/
public class CountWorker {
AtomicInteger sum = new AtomicInteger(0);
@Test
public void testWorker() {
for (int j = 0; j < 10; j++) {
CountWorker countWorker = new CountWorker();
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
countWorker.sum.incrementAndGet();
}
}.start();
}
while (Thread.activeCount() > 1)
// 保证前面的线程都执行完
Thread.yield();
System.out.println(countWorker.sum);
}
}
}
10次计算,每次消耗的时间 :
elapse time is : 7
elapse time is : 3
elapse time is : 2
elapse time is : 3
elapse time is : 9
elapse time is : 2
elapse time is : 3
elapse time is : 3
elapse time is : 4
elapse time is : 34
三、使用countDownLatch 代替Thread.activeCount()判断
package thread.worker;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by hl on 2017/8/17.
*/
public class CountWorker {
private int unsafeSum = 0;
private synchronized void increase(CountWorker countWorker) {
countWorker.unsafeSum++;
}
@Test
public void testWorker() throws InterruptedException {
for (int j = 0; j < 10; j++) {
CountWorker countWorker = new CountWorker();
CountDownLatch countLatch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
increase(countWorker);
countLatch.countDown();
}
}.start();
}
countLatch.await();
System.out.println(countWorker.unsafeSum);
}
}
}
10次计算,每次消耗的时间 :
elapse time is : 1011
elapse time is : 1002
elapse time is : 1005
elapse time is : 1001
elapse time is : 1001
elapse time is : 1002
elapse time is : 1003
elapse time is : 1001
elapse time is : 1002
elapse time is : 1001
四、使用lock.lock()代替synchronized
package thread.worker;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by hl on 2017/8/17.
*/
public class CountWorker {
private int unsafeSum = 0;
private void increase(CountWorker countWorker) {
countWorker.unsafeSum++;
}
@Test
public void testWorker() throws InterruptedException {
Lock lock = new ReentrantLock();
for (int j = 0; j < 10; j++) {
CountWorker countWorker = new CountWorker();
CountDownLatch countCount = new CountDownLatch(30);
for (int i = 0; i < 30; i++) {
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
increase(countWorker);
lock.unlock();
countCount.countDown();
}
}.start();
}
countCount.await();
System.out.println(countWorker.unsafeSum);
}
}
}
10次计算,每次消耗的时间 :
elapse time is : 1012
elapse time is : 1019
elapse time is : 1018
elapse time is : 1011
elapse time is : 1013
elapse time is : 1041
elapse time is : 1011
elapse time is : 1013
elapse time is : 1031
elapse time is : 1009
最后说明一下:
while (Thread.activeCount() > 1)
// 保证前面的线程都执行完
Thread.yield();
在Run和Debug两种启动方式下 , Run方式启动会导致线程一直等待 , 目前原因不明 ….