public Object getSth(){
 if(vector.size()==0) wait();
 synchronize(vector){
  Object o = vector.firstElement();
  vector.removeElementAt(0);
  return o;
 }
}public void returnSth(Object sth){
 synchronize(vector){
  vector.add(sth);
  notifyAll();
 }
}

解决方案 »

  1.   

    synchronized (对象 a) { } 这样的写法是不是将这个对象a锁定,只能同时让一个线程使用?
      

  2.   

    那么为什么既然说是从wait()后继续执行代码,为什么书上说建议用while()循环,而不要用if else来判断线程执行的条件啊?
      

  3.   

    public class SyncTest
    extends Thread
    {
        public static class Resource
        {
            public static Resource resource = new Resource();   //共享资源
            boolean resourceInUse = false;
            int count = 0;
            public void setCount( int count ){ this.count = count; }
            public int getCount(){ return count; }
            public Resource get()
            {
                while( resourceInUse )  //*** 试试看用if会如何。
                { 
                    try
                    {
                        synchronized(this)
                        {
                            System.out.println( Thread.currentThread().getName() + " is waiting..." ); 
                            wait(); 
                        }
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                    }
                }
                resourceInUse = true; 
                return this;
            }
            public void ret()
            {
                try
                {
                    resourceInUse = false;
                    synchronized(this)
                    { 
                        System.out.println( Thread.currentThread().getName() + " notified all." ); 
                        notifyAll(); 
                    }
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                }
            }
            public static Resource getResource()
            {
                return resource.get();
            }
            
            public static void returnResource()
            {
                resource.ret();
            }
        };    public void run()
        {
            //取得,计算,设置
            Resource r = Resource.getResource();
            System.out.println( getName() + ": add 10" );
            int rc = r.getCount();
            try
            {
                sleep( 100 );
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
            r.setCount( rc + 10 );
            Resource.returnResource();
        }
        
        public static void main( String args[] )
        throws Exception
        {
            int n = Integer.valueOf(args[0]).intValue();
            System.out.println( "Before " + n + " threads using the resource. count = " + Resource.getResource().getCount() );
            Resource.returnResource();
            SyncTest tests[] = new SyncTest[n];
            for( int i = 0; i < n; i++ )
                tests[i] = new SyncTest();
            for( int i = 0; i < n; i++ )
                tests[i].start();
            for( int i = 0; i < n; i++ )
                tests[i].join();
            System.out.println( "After " + n + " threads used the resource. count = " + Resource.getResource().getCount() );
            Resource.returnResource();
        }
    }
      

  4.   

    上面的程序与原题不一致。
    while应当和notifyAll配合。
    notify,我想用if就可以了。