[JAVA100例]066、线程掌握[Java编程]
本文“[JAVA100例]066、线程掌握[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
/**
* <p>Title: 线程掌握</p>
* <p>Description: 实现对线程的掌握,中止、挂起、恢复、终止</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: 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();
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...........");
}
}
以上是“[JAVA100例]066、线程掌握[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |