日期:2014-05-20  浏览次数:20596 次

关于线程的锁的一个问题
///////////////////Airport.java


import   java.awt.*;
import   java.io.*;
import   java.util.*;


/**
  *   @author   elove
  *
  */
public   class   Airport   {
public   AirportMap   airportMap;
protected   ThreadGroup   tg;
private   static   Vector <Plane>   runwayList=new   Vector <Plane> ();//#2
private   static   Vector <Plane>   gatesList=new   Vector <Plane> ();//#1


public   Airport(){
    Frame   mapFrame;
    Panel   pl;
    mapFrame=new   Frame( "Airport   Map ");
    airportMap=new   AirportMap();
    pl=new   Panel();
    pl.setLayout(new   BorderLayout());
    pl.add( "Center ",   airportMap);
    mapFrame.add( "Center ",   pl);
    mapFrame.setSize(200,   300);
    mapFrame.pack();
 
    tg=   new   ThreadGroup( "Threads ");
//   Old
   
    /*
    *  
    Plane   planes[]=new   Plane[1];
    planes[0]=new   Plane(tg,0,this);
   
    try{
planes[0].join();    
    }catch(InterruptedException   e){
    e.printStackTrace();
    }
    System.exit(0);*/
   
   
   
    //       New
   
    Plane   planes[]=new   Plane[2];
    for(int   j=0;j <2;j++){
    planes[j]=new   Plane(tg,j,this);
    }
    synchronized(this){
    try{
    wait(5000);
    }catch(InterruptedException   e){
    e.printStackTrace();
    }
    }
    System.exit(0);
}


      private   void   RequestLock(Plane   pl,Vector <Plane>   locklist){
      boolean   empty;
   
     
      synchronized(locklist){
     
      empty=locklist.isEmpty();
      locklist.addElement(pl);
     
      }
     
      if(empty==false){
        try{
        pl.wait();
        }
       
      catch(InterruptedException   e){e.printStackTrace();}
      }
}  

     
     
      public   void   ReleaseLock(Plane   pl,Vector <Plane>   lockList){
      Plane   peek;//   To   notify   a   plane!
      synchronized(lockList){
    lockList.removeElement(pl);
    if   (lockList.isEmpty()==false){
    try{
    peek=lockList.firstElement();