使用synchronized和Condition三线程轮流打印A,B,C示例

ReentrantLock并不是一种替代内置加锁的方法,而是当内置加锁机制不适用时,作为一种可选择的高级功能。当需要一些高级功能时才使用ReentrantLock,包括定时的锁等待,可中断的锁等待,公平性,锁绑定多个条件以及实现非块结构的加锁。否则,还是应该优先使用内置锁synchronized。synchronized是JVM的内置特性,可以进行一些优化。比如对线程封闭锁对象的锁消除优化,通过增加锁粒度来消除内置锁的同步等。

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

public class MultiThreadPrint
{

public static void main(String[] args)
{
init();
}

private static void init()
{
ExecutorService executor = Executors.newFixedThreadPool(3);
final Print print = new SynchronizedPrint();
// final Print print = new ConditionPrint();

executor.execute(new Runnable()
{
@Override
public void run()
{
try
{
Thread.currentThread().setName("thread A");
print.printA();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
executor.execute(new Runnable()
{
@Override
public void run()
{
try
{
Thread.currentThread().setName("thread B");
print.printB();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});

executor.execute(new Runnable()
{
@Override
public void run()
{
try
{
Thread.currentThread().setName("thread C");

print.printC();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
try
{
Thread.sleep(20*1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
executor.shutdownNow();

}

interface Print
{
void printA() throws InterruptedException;

void printB() throws InterruptedException;

void printC() throws InterruptedException;

}

static class SynchronizedPrint implements Print
{
int flag = 1;

@Override
public void printA() throws InterruptedException
{
while (true)
{
synchronized (this)
{
while (flag != 1)
{
this.wait();
}
System.out.println(Thread.currentThread().getName() + " prints " + "A");
Thread.sleep(800);
flag = 2;
notifyAll();
}
}

}

@Override
public void printB() throws InterruptedException
{
while (true)
{
synchronized (this)
{
while (flag != 2)
{
this.wait();
}
System.out.println(Thread.currentThread().getName() + " prints " + "B");
Thread.sleep(800);
flag = 3;
notifyAll();
}
}
}

@Override
public void printC() throws InterruptedException
{
while (true)
{
synchronized (this)
{
while (flag != 3)
{
this.wait();
}
System.out.println(Thread.currentThread().getName() + " prints " + "C");
Thread.sleep(800);
flag = 1;
notifyAll();
}
}

}

}

static class ConditionPrint implements Print
{
final ReentrantLock lock = new ReentrantLock();
final Condition cond1 = lock.newCondition();
final Condition cond2 = lock.newCondition();
final Condition cond3 = lock.newCondition();
int flag = 1;

public void printA() throws InterruptedException
{
while (true)
{
lock.lock();
try
{
while (flag != 1)
{
cond1.await();
}
System.out.println(Thread.currentThread().getName() + " prints " + "A");
Thread.sleep(800);
flag = 2;
cond2.signal();
}
finally
{
lock.unlock();
}
}
}

public void printC() throws InterruptedException
{
while (true)
{
lock.lock();
try
{
while (flag != 2)
{
cond2.await();
}
System.out.println(Thread.currentThread().getName() + " prints " + "B");
Thread.sleep(800);
flag = 3;
cond3.signal();
}
finally
{
lock.unlock();
}
}

}

public void printB() throws InterruptedException
{
while (true)
{
lock.lock();
try
{
while (flag != 3)
{
cond3.await();
}
System.out.println(Thread.currentThread().getName() + " prints " + "C");
Thread.sleep(800);
flag = 1;
cond1.signal();
}
finally
{
lock.unlock();
}
}
}
}
}


控制台输出:
thread A prints A
thread B prints B
thread C prints C
thread A prints A
thread B prints B
thread C prints C
thread A prints A
thread B prints B
thread C prints C
。。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值