二、方法await()/signal()
在JDK5.0以后,JAVA提供了新的更加健壮的线程处理机制,包括了同步、锁定、线程池等等,可以实现更小粒度上的控制。await()和signal()就是其中用来同步的两种方法,功能基本上和wait()/notify()相同,完全可以取代它们,但是它们和新引入的锁定机制Lock直接挂钩,具有更大的灵活性。
import java.util.LinkedList;
import java.util.concurrent.locks.*;
public class Sycn2
{
private LinkedList myList = new LinkedList();
private int MAX = 10;
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
public void start()
{
new Thread(new Producer())。start();
new Thread(new Consumer())。start();
}
public static void main(String[] args) throws Exception
{
Sycn2 s2 = new Sycn2();
s2.start();
}
class Producer implements Runnable
{
public void run()
{
while (true)
{
lock.lock();
try
{