需求:3个线程 输出ABC ------> ABCABCABC。。。。。此类型
1、 使用线程池 将所有线程放入一个队列 ,保证顺序输出
public class ThreeThread {
public static void main(String[] args) throws InterruptedException {
//用线程池来实现 ,3个线程加入线程池
ExecutorService pool = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
pool.submit(()-> System.out.println("AAAAAA"));
pool.submit(()-> System.out.println("BBBBBB"));
pool.submit(()-> System.out.println("CCCCCC"));
}
pool.shutdown();
}
}
2、使用 wait(), synchronized(同步锁) 轮询机制 到谁了 谁输出
public class ThreeThread {
public static void main(String[] args) throws InterruptedException {
Param param = new Param();//A开始打印
new Thread(new Letter(param, "A", 0)).start();