1.start状态:

package com.roocon.thread.t1;
public class NewThread implements Runnable {

    @Override
    public void run() {
        System.out.println("线程运行了");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new NewThread());//创建线程并且指定线程任务
        thread.start();//启动线程
    }
}

start、就绪、运行状态的demo演示

 

2.:线程启动后,进入就绪状态,自定义线程和主线程交互运行,谁先获得cpu,则谁就进入运行状态,输出对应的消息。

package com.roocon.thread.t1;
public class NewThread implements Runnable {

    @Override
    public void run() {
        while(true){
            System.out.println("自定义线程运行了");
        }
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new NewThread());//创建线程并且指定线程任务
        thread.start();//启动线程
        while(true){
            System.out.println("主线程运行了");
        }
    }
}

运行结果:自定义线程运行了(连续输出一段时间);主线程运行了(连续输出一段时间);自定义线程运行了(连续输出一段时间);主线程运行了(连续输出一段时间)......

start、就绪、运行状态的demo演示

 

相关文章:

  • 2021-10-10
  • 2021-10-07
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-08-04
猜你喜欢
  • 2021-07-11
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
相关资源
相似解决方案