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