需求:3个线程 输出ABC ------> ABCABCABC。。。。。此类型
ps:2020-12-19 更新了6种
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("A"));
pool.submit(()-> System.out.println("B"));
pool.submit(()-> System.out.println("C"));
}
pool.shutdown();
}
}
2、java.util.concurrent.atomic.AtomicInteger 保证原子操作
/**.
* Created by: yuqiang
* Date: 2020/12/18 10:54
* Description: java.util.concurrent.atomic.AtomicInteger 控制
*/
public class Foo {
private AtomicInteger flag = new AtomicInteger(1);
public Foo() { }
public void first(String str) throws InterruptedException {
while(flag.get()%3 != 1){ };
System.out.println(str);
flag.incrementAndGet();
}
public void second(String str) throws InterruptedException {
while(flag.get()%3 != 2){ };
System.out.println(str);
flag.incrementAndGet();
}
public void third(String str) throws InterruptedException {
while(flag.get()%3 != 0){ };
System.out.println(str);
flag.incrementAndGet();
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 3; i++) {
Thread.sleep(100);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 3; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 3; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
3、volatile 控制flag在并发环境中可见
/**.
* Created by:yuqiang
* Date: 2020/12/18 10:54
* Description: volatile 控制flag在并发环境中可见
*/
public class Foo {
private volatile int flag = 1;
public Foo() {
}
public void first(String str) throws InterruptedException {
while(flag%3 != 1){
};
System.out.println(str);
flag++;
}
public void second(String str) throws InterruptedException {
while(flag%3 != 2){
};
System.out.println(str);
flag++;
}
public void third(String str) throws InterruptedException {
while(flag%3 != 0){
};
System.out.println(str);
System.out.println("==============");
flag++;
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
4、synchronized wait() notifyAll() 控制
/**
* Created by: e-yuqiang1
* Date: 2020/12/18 10:54
* Description: synchronized wait() notifyAll()
*/
public class Foo {
private int flag = 1;
public Foo() {
}
public synchronized void first(String str) throws InterruptedException {
while(flag%3 != 1){
wait();
};
System.out.println(str);
flag++;
notifyAll();
}
public synchronized void second(String str) throws InterruptedException {
while(flag%3 != 2){
wait();
};
System.out.println(str);
flag++;
notifyAll();
}
public synchronized void third(String str) throws InterruptedException {
while(flag%3 != 0){
wait();
};
System.out.println(str);
System.out.println("=====================");
flag++;
notifyAll();
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
5、LockReentrantLock Condition(await(),signalAll()) 控制
/**
* Created by: e-yuqiang1
* Date: 2020/12/18 10:54
* Description:
*/
public class Foo {
private int flag = 1;
private Lock lock;
private Condition condition;
public Foo() {
lock = new ReentrantLock();
condition = lock.newCondition();
}
public void first(String str) throws InterruptedException {
lock.lock();
try {
while(flag%3 != 1){
condition.await();
}
System.out.println(str);
flag++;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void second(String str) throws InterruptedException {
lock.lock();
try {
while(flag%3 != 2){
condition.await();
}
System.out.println(str);
flag++;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void third(String str) throws InterruptedException {
lock.lock();
try {
while(flag%3 != 0){
condition.await();
}
System.out.println(str);
System.out.println("=====================");
flag++;
condition.signalAll();
} finally {
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
6、信号量 semaphore(.acquire() .release())
/**
* Created by: e-yuqiang1
* Date: 2020/12/18 10:54
* Description:
*/
public class Foo {
private Semaphore semaphoreA;
private Semaphore semaphoreB;
private Semaphore semaphoreC;
public Foo() throws InterruptedException {
semaphoreA = new Semaphore(1);
semaphoreB = new Semaphore(1);
semaphoreC = new Semaphore(1);
semaphoreB.acquire();//提前请求B
semaphoreC.acquire();//提前请求C
}
public void first(String str) throws InterruptedException {
semaphoreA.acquire();
System.out.println(str);
semaphoreB.release();
}
public void second(String str) throws InterruptedException {
semaphoreB.acquire();
System.out.println(str);
semaphoreC.release();
}
public void third(String str) throws InterruptedException {
semaphoreC.acquire();
System.out.println(str);
System.out.println("=====================");
semaphoreA.release();
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
7、CountDownLatch(.await() .countDown()) 计数器(减法)
ps:感觉比较取巧 不推荐这种写法
/**
* Created by: e-yuqiang1
* Date: 2020/12/18 10:54
* Description: 写的挺Low的 试了半个小时
*/
public class Foo {
private CountDownLatch countDownLatchA;
private CountDownLatch countDownLatchB;
private CountDownLatch countDownLatchC;
public Foo() throws InterruptedException {
countDownLatchA = new CountDownLatch(1);
countDownLatchB = new CountDownLatch(1);
countDownLatchC = new CountDownLatch(1);
countDownLatchA.countDown();
}
public void first(String str) throws InterruptedException {
countDownLatchA.await();
System.out.println(str);
countDownLatchA= new CountDownLatch(1);
countDownLatchB.countDown();
}
public void second(String str) throws InterruptedException {
countDownLatchB.await();
System.out.println(str);
countDownLatchB = new CountDownLatch(1);
countDownLatchC.countDown();
}
public void third(String str) throws InterruptedException {
countDownLatchC.await();
System.out.println(str);
System.out.println("=====================");
countDownLatchC = new CountDownLatch(1);
countDownLatchA.countDown();
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
8、BlockingQueue(.take() .put()) 阻塞队列控制
/**
* Created by: e-yuqiang1
* Date: 2020/12/18 10:54
* Description: BlockingQueue 阻塞队列
*/
public class Foo {
private BlockingQueue<Integer> blockingQueueA;
private BlockingQueue<Integer> blockingQueueB;
private BlockingQueue<Integer> blockingQueueC;
public Foo() throws InterruptedException {
blockingQueueA = new ArrayBlockingQueue<>(1);
blockingQueueB = new ArrayBlockingQueue<>(1);
blockingQueueC = new ArrayBlockingQueue<>(1);
blockingQueueA.put(1);
}
public void first(String str) throws InterruptedException {
blockingQueueA.take();
System.out.println(str);
blockingQueueB.put(2);
}
public void second(String str) throws InterruptedException {
blockingQueueB.take();
System.out.println(str);
blockingQueueC.put(3);
}
public void third(String str) throws InterruptedException {
blockingQueueC.take();
System.out.println(str);
System.out.println("=====================");
blockingQueueA.put(1);
}
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
foo.second("B");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(300);
foo.first("A");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
foo.third("C");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
目前想到了只有这8种......