private static final Integer MAX_COUNT = 6;
private static String[] chars = {“a”, “b”, “c”};
private String name;
public AtomicIntegerThread(String name,AtomicInteger currentCount) {
this.name = name;
this.currentCount = currentCount;
}
@Override
public void run() {
while (currentCount.get() < MAX_COUNT) {
if (this.name.equals(chars[currentCount.get() % 3])) {
printAndPlusOne(this.name);
}
}
}
public void printAndPlusOne(String content) {
System.out.print(content);
currentCount.getAndIncrement();
}
public static void main(String[] args) {
AtomicInteger currentCount = new AtomicInteger(0);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 20, TimeUnit.MINUTES, new LinkedBlockingQueue());
threadPoolExecutor.execute(new AtomicIntegerThread(“a”,currentCount));
threadPoolExecutor.execute(new AtomicIntegerThread(“b”,currentCount));
threadPoolExecutor.execute(new AtomicIntegerThread(“c”,currentCount));
threadPoolExecutor.shutdown();
}
}

这里一定要注意有一个坑,如果线程池核心线程数少于3个,1个或者2个,上述代码会有问题,因为线程池的队列是无界队列,多余核心线程数的任务会被放到队列中,这样循环打印的时候,有一些线程不是在运行中的,是在队列里的,运行中的线程会被阻塞,导致相互等待,死锁问题出现。所以线程池的核心线程数必须等于大于3个,这样保证有3个线程一直在运行。

2、volatile静态变量控制

还有一种就是将某个变量设置为静态,这样线程本身也共享该变量,则可以对其操作。但要注意给这个静态变量加上volatile字段,这个字段可以帮助多线程中的可见性。代码如下:

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
• 三个线程按次序轮流打印a,b,c
• 
*/
public class StaticVarThread implements Runnable {
private static volatile Integer currentCount = 0;
private static final Integer MAX_COUNT = 6;
private static String[] chars = {“a”, “b”, “c”};
private String name;
public StaticVarThread(String name) {
this.name = name;
}
@Override
public void run() {
while (currentCount < MAX_COUNT) {
try {
while (this.name.equals(chars[currentCount % 3]) && currentCount < MAX_COUNT) {
printAndPlusOne(this.name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
public void printAndPlusOne(String name) {
System.out.println(name + “\t” + currentCount);
currentCount++;
}
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 20, TimeUnit.MINUTES, new LinkedBlockingQueue());
threadPoolExecutor.execute(new StaticVarThread(“a”));
threadPoolExecutor.execute(new StaticVarThread(“b”));
threadPoolExecutor.execute(new StaticVarThread(“c”));
threadPoolExecutor.shutdown();
}
}

3、synchronized对象加锁

利用一个计数来除线程个数得到余数从而控制线程输出,并且利用synchronized、一个Object的wait()和notify()方法来对线程的阻塞和唤醒,这里的技巧可以看看。

  • wait()

阻塞当前线程,等待被释放,继续执行当前线程

  • notify()

唤醒监视该对象的第一个线程,notifyAll()唤醒监视该对象的所有线程。

import org.junit.Test;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SynchronizedThread {