多线程实现按序打印

LeetCode1114

lock-condition的wait()和signal()
import java.util.concurrent.locks.Condition; 
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Foo {
	int num;
	Lock lock;
	//精确的通知和唤醒线程
	Condition condition1, condition2, condition3;
  
	public Foo() { 
    num = 1;
		lock = new ReentrantLock(); 
    condition1 = lock.newCondition(); 
    condition2 = lock.newCondition(); condition3 = lock.newCondition();
	}
  
	public void first(Runnable printFirst) throws InterruptedException { 
    lock.lock();
		try {
			while (num != 1) { 
        condition1.await();
			}
      printFirst.run();
			num = 2;
			condition2.signal();
		} catch (Exception e) {
      e.printStackTrace();
		} finally { 
      lock.unlock();
		} 
  }
  
	public void second(Runnable printSecond) throws InterruptedException { 
    lock.lock();
    try {
      while (num != 2) { 
        condition2.await();
      }
      printSecond.run(); 
      num = 3; 
      condition3.signal();
    } catch (Exception e) { 
      e.printStackTrace();
    } finally { 
      lock.unlock();
    } 
  }
  public void third(Runnable printThird) throws InterruptedException { 
    lock.lock();
    try {
      while (num != 3) { 
        condition3.await();
      }
     	printThird.run();
      num = 1;
      condition1.signal();
    } catch (Exception e) { 
      e.printStackTrace();
    } finally { 
      lock.unlock();
    } 
  }
}
信号量Semaphore
import java.util.concurrent.Semaphore;
class Foo {
  Semaphore semaphore12, semaphore23;
  public Foo() { 
    //初始的允许请求均设为0 
    semaphore12 = new Semaphore(0); 
    semaphore23 = new Semaphore(0);
  }
  
  public void first(Runnable printFirst) throws InterruptedException {
		printFirst.run();
  	//释放⼀一个12的信号量量
  	semaphore12.release();
   }
  
  public void second(Runnable printSecond) throws InterruptedException { 
    //获取⼀一个12的信号量量,没有则阻塞
  	semaphore12.acquire();
		printSecond.run();
  	//释放⼀一个23的信号量量
  	semaphore23.release(); 
  }
  
  public void third(Runnable printThird) throws InterruptedException { 
    //获取⼀一个23的信号量量,没有则阻塞
  	semaphore23.acquire();
  	printThird.run();
  } 
}
使用synchronized
public class Foo {
  private volatile int flag = 1;
  private final Object object = new Object();
  public Foo() {
  }
  
  public void first(Runnable printFirst) throws InterruptedException { 
    synchronized (object) {
      while (flag != 1) {
        object.wait(); 
      }
      printFirst.run();
      flag = 2;
      object.notifyAll();
    } 
  }
  
  public void second(Runnable printSecond) throws InterruptedException { 
    synchronized (object) {
      while (flag != 2) {
        object.wait(); 
      }
      printSecond.run();
      flag = 3;
      object.notifyAll();
    }
  }
  
  public void third(Runnable printThird) throws InterruptedException { 
    synchronized (object) {
      while (flag != 3) {
        object.wait(); 
      }
      printThird.run(); 
    }
  }
}
使用CountDownLatch
import java.util.concurrent.CountDownLatch;
class Foo {
  CountDownLatch countDownLatch12, countDownLatch23;
  public Foo() {
  	countDownLatch12 = new CountDownLatch(1); 
    countDownLatch23 = new CountDownLatch(1);
  }
  
  public void first(Runnable printFirst) throws InterruptedException {
  	printFirst.run();
  	countDownLatch12.countDown();
  }
  
  public void second(Runnable printSecond) throws InterruptedException { 
    //等待计数器器归零再向下执⾏
  	countDownLatch12.await();
		printSecond.run();
  	countDownLatch23.countDown(); 
  }

   public void third(Runnable printThird) throws InterruptedException { 
     //等待计数器器归零再向下执⾏
     countDownLatch23.await();
     printThird.run();
   } 
}
使用AtomicInteger
class Foo {
  private AtomicInteger firstJobDone = new AtomicInteger(0); 
  private AtomicInteger secondJobDone = new AtomicInteger(0);
  public Foo() {
  }
  public void first(Runnable printFirst) throws InterruptedException { 
    // printFirst.run() outputs "first".
    printFirst.run();
  	// mark the first job as done, by increasing its count. 
    firstJobDone.incrementAndGet();
  }
  
  public void second(Runnable printSecond) throws InterruptedException { 
    while (firstJobDone.get() != 1) {
  	// waiting for the first job to be done. 
    }
  	// printSecond.run() outputs "second". 
    printSecond.run();
  	// mark the second as done, by increasing its count. 
    secondJobDone.incrementAndGet();
  }
  
  public void third(Runnable printThird) throws InterruptedException { 
    while (secondJobDone.get() != 1) {
  	// waiting for the second job to be done. 
    }
  	// printThird.run() outputs "third".
  	printThird.run(); 
  }
}
上一篇:SpringMVC拦截器


下一篇:55. 跳跃游戏