zoukankan      html  css  js  c++  java
  • 中断线程

    package xiancheng.interrup;
    /*
     * 当一个线程运行时,另外一个线程可以将其中断
     * Thread void    interrupt()  中断线程。
     */
    class XianChenInterrupt implements Runnable{
        public void run(){
            System.out.println("开始启动了!");
            try{
            Thread.sleep(10000);
            System.out.println("线程休眠结束了!");
            }catch(Exception e){
             System.out.println("线程被中断了!");
            }
        }
    }
    public class InterruptDemo {
    public static void main(String[] args) {
        XianChenInterrupt xci =new XianChenInterrupt();
        Thread t = new Thread(xci);
        t.start();
        //主线程也来休眠一下
        try {Thread.sleep(500);}
          catch(Exception e){
              System.out.println(e);
          }
        //mian主线程休眠500毫秒之后中断子线程
        t.interrupted();
                }
    }
  • 相关阅读:
    MongoDB小结07
    MongoDB小结07
    MongoDB小结06
    MongoDB小结05
    MongoDB小结04
    MongoDB小结03
    MongoDB小结02
    MongoDB小结01
    this与super
    UVa 11174
  • 原文地址:https://www.cnblogs.com/yuanyuan2017/p/6944975.html
Copyright © 2011-2022 走看看