[JAVA100例]066、线程控制

骑王八逮飞机

骑王八逮飞机

2016-02-19 17:03

下面是个简单易学的[JAVA100例]066、线程控制教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

  

/**
 * pTitle: 线程控制/p
 * pDescription: 实现对线程的控制,中断、挂起、恢复、停止/p
 * pCopyright: Copyright (c) 2003/p
 * pFilename: threadCtrl.java/p
 * @version 1.0
 */
public class threadCtrl{
 public static void main(String [] main){
   new threadCtrl();
 }
/**
 *br方法说明:构造器,控制其它线程
 *br输入参数:
 *br返回类型:
 */
 threadCtrl(){
  try{
   Thread tm = Thread.currentThread();
   threadchild td = new threadchild();
   td.start();
   tm.sleep(500);
   System.out.println("interrupt child thread");
   td.interrupt();

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  System.out.println("let child thread wait!");
   //td.wait();
   //td.suspend();
   tm.sleep(1000);


  System.out.println("let child thread working");
   td.fauxresume();
   //td.resume();
   tm.sleep(1000);
   td.runflag = false;
   System.out.println("main over..............");
  }catch(InterruptedException ie){
   System.out.println("inter main::"+ie);
  }catch(Exception e){
   System.out.println("main::"+e);
  }
 }


  }
/**
 *br类说明:被控线程类
 */
 class threadchild extends Thread {
  boolean runflag = true;
  boolean suspended = true;
  threadchild(){
  }
  public synchronized void fauxresume(){
   suspended = false;
   notify();
  }
  public void run(){
   while(runflag){
    System.out.println("I am working..............");
    try{
     sleep(1000);
    }catch(InterruptedException e){
     System.out.println("sleep::"+e);
    }
    synchronized(this){
    try{
     if(suspended)
      wait();
    }catch(InterruptedException e){
     System.out.println("wait::"+e);
    }
    }
   }
   System.out.println("thread over...........");
  }
 }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

[JAVA100例]066、线程控制

编程语言 网络编程
[JAVA100例]066、线程控制

[JAVA100例]002、Java流程控制

编程语言 网络编程
[JAVA100例]002、Java流程控制

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

[JAVA100例]063、线程群组

编程语言 网络编程
[JAVA100例]063、线程群组

[JAVA100例]065、线程同步

编程语言 网络编程
[JAVA100例]065、线程同步

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

[JAVA100例]062、多线程

编程语言 网络编程
[JAVA100例]062、多线程

[JAVA100例]064、线程间通讯

编程语言 网络编程
[JAVA100例]064、线程间通讯

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Java反编译的研究

Java反编译的研究

用CSS建设网站的实例

用CSS建设网站的实例
下拉加载更多内容 ↓