1.使用synchronized悲观锁
(秋招阿里的一个笔试题,应该写的比较复杂,然后就没有然后了o(╥﹏╥)o)

public class ThreadThreadp {
private int flag = 0;
public synchronized void printa() throws InterruptedException {
while (true)
{
if(flag ==0)
{
System.out.print("A");
flag = 1;
notifyAll();
}
wait();
}
}
public synchronized void printb() throws InterruptedException {
while (true)
{
if(flag ==1)
{
System.out.print("B");
flag = 2;
notifyAll();
}
wait();
}
}
public synchronized void printc() throws InterruptedException {
while (true) {
if (flag == 2) {
System.out.print("C");
Thread.sleep(1000);
flag = 0;
notifyAll();
}
wait();
}
}
public static void main(String[]args) throws InterruptedException
{
ThreadThreadp t = new ThreadThreadp();
PrintA printA = new PrintA(t);
PrintB printB = new PrintB(t);
PrintC printC = new PrintC(t);
Thread t1 = new Thread(printA);
Thread t2 = new Thread(printB);
Thread t3 = new Thread(printC);
t1.start();
t2.start();
t3.start();
    //Thread t11 = new Thread(printA);
    //Thread t21 = new Thread(printB);
    //Thread t31 = new Thread(printC);
    //t11.start();
    //t21.start();
    //t31.start(); } } class PrintA implements Runnable{
private ThreadThreadp t;
PrintA(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printa();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class PrintB implements Runnable{ private ThreadThreadp t;
PrintB(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printb();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class PrintC implements Runnable{
private ThreadThreadp t;
PrintC(ThreadThreadp t){
this.t=t;
}
@Override
public void run() {
try {
t.printc();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

2.使用Lock+Condition

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class threadsan {
public static void main(String [] args)
{
PrintABC printABC = new PrintABC();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printA();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printB();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
printABC.printC();
}
}).start();
}
} class PrintABC{
private final Lock lock = new ReentrantLock();
private Condition lockA = lock.newCondition();
private Condition lockB = lock.newCondition();
private Condition lockC = lock.newCondition();
int flag = 0; public void printA()
{
lock.lock();
try {
while (true)
{
while (flag!=0)
lockA.await();
System.out.print("A");
flag =1;
lockB.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printB()
{
lock.lock();
try {
while (true)
{
while (flag!=1)
lockB.await();
System.out.print("B");
flag =2;
lockC.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printC()
{
lock.lock();
try {
while (true)
{
while (flag!=2)
lockC.await();
System.out.print("C");
Thread.sleep(1000);
flag =0;
lockA.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}

3.使用Semaphore实现
//semaphore没用过。。。参考

import java.util.concurrent.Semaphore;

/**
* Created by huali on 2018/7/25.
*/
public class PrintABCRotationUsingSemaphore {
public static void main(String[] args) {
PrintABCUsingSemaphore printABC = new PrintABCUsingSemaphore();
new Thread(() -> printABC.printA()).start();
new Thread(() -> printABC.printB()).start();
new Thread(() -> printABC.printC()).start();
}
} class PrintABCUsingSemaphore {
private Semaphore semaphoreA = new Semaphore(1);
private Semaphore semaphoreB = new Semaphore(0);
private Semaphore semaphoreC = new Semaphore(0);
  //private int attempts = 0; public void printA() {
print("A", semaphoreA, semaphoreB);
} public void printB() {
print("B", semaphoreB, semaphoreC);
} public void printC() {
print("C", semaphoreC, semaphoreA);
} private void print(String name, Semaphore currentSemaphore, Semaphore nextSemaphore) {
for (int i = 0; i < 10; ) {
try {
currentSemaphore.acquire();
          //System.out.println(Thread.currentThread().getName()+" try to print "+name+", attempts : "+(++attempts));
System.out.println(Thread.currentThread().getName() +" print "+ name);
i++;
nextSemaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

另外参考链接 三个线程轮流执行顺序打印ABC(一):使用Semaphore实现
使用信号量Semaphore循环打印ABC
三个线程轮流执行顺序打印ABC(二):使用Lock+Condition实现
三个线程轮流执行顺序打印ABC(三):使用Lock实现
————————————————
版权声明:本文为CSDN博主「Angel_Zhl」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_33915826/article/details/81205938

最新文章

  1. 开源网站.NETMVC+ Layui+SqlSugar+RestSharp
  2. mac java 安装路径
  3. openoffice
  4. VS2012调试时无法启动程序和拒绝访问问题汇总
  5. mysql乱码以及Data too long for column全解(最完整实用版)
  6. &quot;大哥,割草机借我用一下,我修整一下草坪。&quot; ---- 谈谈this与JavaScript函数调用的不解之缘
  7. Java知识总结--JDBC&amp;XML
  8. 使用javaservice 将jboss 注册为服务
  9. 第一百零六节,JavaScript变量作用域及内存
  10. mysql 4 索引的优缺点
  11. Ubuntu 18.04 LTS修改 国内源(以中科大源为例)
  12. Python杂记
  13. (转)MERGE语法详解
  14. python实现文件的复制
  15. mysql命令行创建表,插入表数据
  16. HNOI2017单旋
  17. JS实战
  18. POJ 2337 Catenyms (欧拉回路)
  19. Spring Cloud Sleuth服务跟踪
  20. &lt;? extends A&gt; 和 &lt;? super A&gt; 的意思

热门文章

  1. Geohash 基本知识及 .NET 下计算相邻8个区域编码
  2. MybatisX idea 快速开发插件
  3. 使用itchat进行自动微信聊天
  4. Mongodb 分片 手动维护chunk
  5. NetworkX系列教程(4)-设置graph的信息
  6. 关于.eslintrc.js代码检测的一些配置
  7. P1069 细胞分裂——数学题,质因数分解
  8. Mac下的PHP的配置与运行
  9. ZR#988
  10. centos 7 启动docker失败。