public synchronized int waitForRest() throw InterruptedException问题出在 throw       想抛出异常正确写法为 throws

解决方案 »

  1.   

    //我kao,伙计,太离谱了吧。
    package test;
    import java.lang.*;public class Barrier{
            private int threads2Wait4;
            private InterruptedException iex;        public Barrier(int nThreads)
            {
                    threads2Wait4 = nThreads;
            }//        public synchronized int waitForRest() throw InterruptedException
            public synchronized int waitForRest() throws InterruptedException
            {
                    int threadNum = --threads2Wait4;                if(iex!= null) throw iex;
    //                if(thread2Wait4<=0){
                    if(threads2Wait4<=0){
                            notifyAll();
                            return threadNum;
                    }
                    while(threads2Wait4 > 0){
                            if(iex!=null) throw iex;
                            try{
                                    wait();
                            }catch(InterruptedException ex){
                                    iex = ex;
                                    notifyAll();
                            }
                    }
                    return threadNum;
            }        public synchronized void freeAll()
            {
                    iex = new InterruptedException("Barrier Released by freeAll");
                    notifyAll();
            }
    }
      

  2.   

    public synchronized int waitForRest() throws InterruptedException  ///这句改一点
    {