线程监控

来源:互联网 发布:c2运输机知乎 编辑:程序博客网 时间:2024/05/29 09:13
public class Al {public static void main(String[] args) {MyThread mt=new MyThread("线程MyThread");                //创建线程类MyThread的对象//System.out.println("-----------------------------------");System.out.println("线程MyThread 是否处于运行状态:"+mt.t.isAlive()); //查看线程的状态,判断它是否处于运行状态//try{  //等待线程结束//System.out.println("-----------------------------------");System.out.println("等待线程结束...");mt.t.join();}catch(InterruptedException e){System.out.println("出现错误,线程中段!");}System.out.println("-----------------------------------");System.out.println("线程MyThread 是否处于运行状态:"+mt.t.isAlive());System.out.println("-----------------------------------");System.out.println("线程正在退出...");}}class MyThread implements Runnable{                   //创建线程类MyThread,实现Runnable接口//String name;Thread t;MyThread(String th){name=th;t=new Thread(this,th);                                          //创建属于类Thread的线程对象//System.out.println("创建线程:"+th);t.start();        //启动线程//}public void run(){ //重写方法run(),线程休眠一段时间后退出//try{Thread.sleep(1000);}catch(InterruptedException e){System.out.println(name+"中断");}System.out.println("-----------------------------------");System.out.println(name+"正在退出...");}}