synchronized, wait(), notify()

解决方案 »

  1.   

    java早期支持线程的suspend,resume和stop方法
    但是现在已经废除了,所以需要自己写扩展类class CustomThread extends Thread 
    {
        volatile boolean goFlag = true;
        
        CustomThread(String name) 
        {
            super(name);
            start(); 
        }    public void run() 
        {
            try {
                for(int loop_index = 0; loop_index <= 5; loop_index++) {
                    System.out.println(Thread.currentThread().getName() + " here...");
                    Thread.sleep(500);
                    synchronized(this) {
                        while(!goFlag) {
                            wait();
                        }
                    }
                }
            } catch (InterruptedException e) {}
        }    public void newSuspend() 
        {
            goFlag = false;
        }    synchronized public void newResume() 
        {
            goFlag = true;
            notify();
        }
    }class suspend 
    {
        public static void main(String args[]) 
        {
            CustomThread thread1 = new CustomThread("one");
            CustomThread thread2 = new CustomThread("two");        try {
                Thread.sleep(1000);
                System.out.println("Suspending thread one...");
                thread1.newSuspend();
                Thread.sleep(1000);
                System.out.println("Resuming thread one...");
                thread1.newResume();
            } catch (InterruptedException e) {}        try {
                thread1.join();
                thread2.join();
            } catch (InterruptedException e) {}
        }
    }
    把程序贴给你,希望对你有帮助