实现ABC交互打印:
使用synchronized
public class RunTest {
private Object a= new Object();
private Object b= new Object();
private Object c= new Object();
@Test
public void get() throws InterruptedException {
SychronizedTest sychronizedTestA = new SychronizedTest(c,a,"A");
SychronizedTest sychronizedTestB = new SychronizedTest(a,b,"B");
SychronizedTest sychronizedTestC = new SychronizedTest(b,c,"C");
Thread threadA = new Thread(sychronizedTestA);
Thread threadB = new Thread(sychronizedTestB);
Thread threadC = new Thread(sychronizedTestC);
threadA.start();
threadB.start();
threadC.start();
Thread.sleep(1000*10);
}
}
public class SychronizedTest implements Runnable{
private Object pre = new Object();
private Object self= new Object();
private String name;
public SychronizedTest(Object pre, Object self, String name) {
this.pre = pre;
this.self = self;
this.name = name;
}
@Override
public void run() {
int i = 0;
while(i<10){
i++;
synchronized (pre){
synchronized (self){
System.out.println(name);
self.notifyAll();
}
/*注意,只有notifyAll()锁在的同步快运行完毕后,才会真正唤醒所有线程,但同
步快没运行结束的时候,同步对象是加锁的*/
try {
pre.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
上诉代码要注意:
每一个线程的pre和self是来自同一个类中的类变量。所以给pre加锁时对其他两个线程是会影响的。
虽然wait会释放锁,但是因为线程进入等待状态,没有出同步块,所以pre的锁没有被释放。
使用lock
public class LockTest{
private static Lock lock = new ReentrantLock();
private static int state = 0;
static class LockA implements Runnable{
@Override
public void run() {
for (int i = 0; i <10 ;) {
try{
lock.lock();
while(state%3==0){
System.out.println("A");
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}
static class LockB implements Runnable{
@Override
public void run() {
for (int i = 0; i <10 ; ) {
try{
lock.lock();
while(state%3==1){
System.out.println("B");
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}
static class LockC implements Runnable{
@Override
public void run() {
for (int i = 0; i <10 ;) {
try{
lock.lock();
while(state%3==2){
System.out.println("C");
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread threadA = new Thread(new LockA());
Thread threadB = new Thread(new LockB());
Thread threadC = new Thread(new LockC());
threadA.start();
threadB.start();
threadC.start();
}
}
当多个线程使用同一个lock时,一个线程对lock进行了加锁,其他线程只能等待该锁被释放后才能对该lock进行加锁,并在lock.lock()处进入等待。