例子十二.txt

来自「这是一本java基础教程 对新手上路有很大帮助」· 文本 代码 · 共 41 行

TXT
41
字号
public class Example8_12{
    public static void main(String args[]){
        MyThread thread=new MyThread();
        thread.setName("张三");
        thread.start();
        while(thread.getStop()==false) {}
        System.out.println("我是主线程,负责恢复"+thread.getName()+"线程"); 
        thread.restart();    //恢复thread线程
    }
} 
class MyThread extends Thread{
    int number=0;
    boolean stop=false;
    boolean getStop(){
           return stop;
    }
    public void run(){
        while(true){
           number++;
           System.out.println(Thread.currentThread().getName()+"的number="+number);
           if(number==3){
              try{  System.out.println(Thread.currentThread().getName()+"被挂起");
                   stop=true;
                   hangUP();//挂起线程
                   System.out.println(Thread.currentThread().getName()+"恢复执行");
              } 
              catch(Exception e){}  
           }
           try{ Thread.sleep(1000); 
           } 
           catch(Exception e){}
        }
    }
    public synchronized void  hangUP() throws InterruptedException{
        wait();  
    }
    public synchronized void  restart(){
      notifyAll();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?