网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  [java100例]066、线程控制     
  文章作者:未知  文章来源:水木森林  
  查看:115次  录入:管理员--2007-11-17  
 

/**
 * <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例]065、线程同步    下一篇: [java100例]067、线程优先级
  相关文档
robocode 高手的秘诀:因数避墙法 11-17
新手入门:初学者搭建j2me移动开发平台 11-16
jive 源代码探索 11-17
java和c++比较(1)--虚函数和指针 11-17
java中实现ip地址的各种表现形式之间的转换 11-17
hibernate的hello word(国内高手撰写) 11-17
泛型类型的子类及通配符的使用 11-17
jsp、servlet关于中文问题再谈 11-17
java 程序中的多线程 11-17
新手入门:java中变量和常量的基本语法 11-16
从java程序中调用其它程序 11-17
java动画编程基础 11-17
java初学者实践教程26-网络程序 11-17
详细讲述soa的发展历史与标准规范 11-17
java基础:jsp处理表单的一些经验分享 11-16
struts1.0学习文档-初学者入门 11-17
在eclipse中扩展refactoringwizard 框架 11-17
session详解 11-17
轻松掌握java泛型(第1部分) 11-17
java语言实用技巧:常用vi高级命令集锦 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息