线程的状态模拟

class MyThread extends Thread{
    public void run(){
        try{
            Thread.sleep(1000);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        
    }
}
class MyThread1 implements Runnable{
    public synchronized void run(){
        try{
            wait(5000); // TIMED_WAITING 记时等待
            //wait();//WAITING  等待
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}


public class Demo10 {
    public static void main(String[] args) {
        MyThread my = new MyThread();
        System.out.println("线程状态my:new:"+my.getState());
        my.start();
        System.out.println("线程状态my:start:"+my.getState());

        MyThread1 my1 = new MyThread1();
        Thread t1 = new Thread(my1);
        t1.start();
        try{
            Thread.sleep(2000);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println("线程状态主线程休眠结束:"+my.getState());
        System.out.println("线程状态t1:"+t1.getState());


    }
}

发布了91 篇原创文章 · 获赞 43 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/kongfanyu/article/details/97283743