[JAVA100例]066、线程控制

2016-02-19 17:03 0 1 收藏

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

【 tulaoshi.com - 编程语言 】

  

/**
 * 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();


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

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

  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);
  }
 }

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

  }
/**
 *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://www.tulaoshi.com/n/20160219/1614033.html

延伸阅读
import java.awt.*; import java.awt.event.*; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; /**  * pTitle: 按钮演示/p  * pDescription: 提供一个按钮的演示。如何实现按钮和是一个按钮失效/p  * pCopyright:...
JDK说明: join public final void join()                 throws InterruptedException等待该线程终止。 抛出: InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除 测试代码: 代码如下: public class MyTh...
/**  * pTitle: 实现Runnable接口,获得线程。/p  * pDescription: 通过实现Runnable接口来获得自己的线程(t2)。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: twothread.java/p  * @version 1.0  */ public class twothread implements Runnable { /**  *br方法说明:构造器。实际线程,并启动...
package e8.com; /**  * pTitle: 标识符/p  * pDescription: 演示标识符对类的访问控制/p  * pCopyright: Copyright (c) 2003/p  * pFilename: /p  * @version 1.0  */ public class classDemo1 { //公有方法  public void mechod1()  { System.out.println("这是一个公有的方法!任何类...
import java.io.*; /**  * pTitle: 读取随机文件/p  * pDescription: 演示使用RandomAccessFile类读取文件。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: RandFile.java/p  * @version 1.0  */ public class RandFile{ /**  *br方法说明:主方法  *br输入参数:  *br返回类型: &...

经验教程

481

收藏

5
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部