多线程(至少三个线程)分别打印A、B、C,要求按ABC的顺序循环打印10次。

时间:2022-04-17 18:35:48

看到网上一道题,三个线程按顺序打印A、B、C循环10次,于是自己按几种方法也写了一下:

一、用线程池、Lock锁和condition(Object 监视器方法的使用)组合使用实现:

package multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestABCThread {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private int count;

public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(3);
TestABCThread abc = new TestABCThread();
for (int i = 0; i < 10; i++) {
executorService.execute(abc.new Run("AAAAAAAAAAAAAAAA", 1));
executorService.execute(abc.new Run("BBBBBBBBBBBBBBBBB", 2));
executorService.execute(abc.new Run("CCCCCCCCCCCCCCcCC", 3));
}
executorService.shutdown();
}

class Run implements Runnable {
private String _name = "";
private int _threadNum;

public Run(String name, int threadNum) {
_name = name;
_threadNum = threadNum;
}

@Override
public void run() {
lock.lock();
try {
while (true) {
if (count % 3 == _threadNum - 1) {
System.out.println("Thread-Name:" + _name);
count++;
condition.signalAll();
break;
} else {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} finally {
lock.unlock();
}
}
}
}


二、用线程池、synchronized关键字和Object 监视器方法组合实现(实现跟第一种大同小异):

package multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestABCThread2 {
private Object lock = new Object();
private int count;

public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(3);
TestABCThread2 abc = new TestABCThread2();
for (int i = 0; i < 10; i++) {
executorService.execute(abc.new Run("AAAAAAAAAAAAAAAA", 1));
executorService.execute(abc.new Run("BBBBBBBBBBBBBBBBB", 2));
executorService.execute(abc.new Run("CCCCCCCCCCCCCCcCC", 3));
}
executorService.shutdown();
}

class Run implements Runnable {
private String _name = "";
private int _threadNum;

public Run(String name, int threadNum) {
_name = name;
_threadNum = threadNum;
}

@Override
public void run() {
synchronized (lock) {
while (true) {
if (count % 3 == _threadNum - 1) {
System.out.println("Thread-Name:" + _name);
count++;
lock.notifyAll();
break;
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}


三、不用线程池,synchronized关键字和Object 监视器方法组合实现(Lock和Condition差不多就不写了):
package multithread;

public class TestABCThread3 {
private Object lock = new Object();
private int count;

public static void main(String[] args) {
TestABCThread3 abc = new TestABCThread3();
new Thread(abc.new Run("AAAAAAAAAAAAAAAA", 1)).start();
new Thread(abc.new Run("BBBBBBBBBBBBBBBBB", 2)).start();
new Thread(abc.new Run("CCCCCCCCCCCCCCcCC", 3)).start();
}

class Run implements Runnable {
private String _name = "";
private int _threadNum;

public Run(String name, int threadNum) {
_name = name;
_threadNum = threadNum;
}

@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (true) {
if (count % 3 == _threadNum - 1) {
System.out.println("Count:" + i + ",Thread-Name:"
+ _name);
count++;
lock.notifyAll();
break;
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}