当前位置: 代码迷 >> 综合 >> 多线程执行时间的差异
  详细解决方案

多线程执行时间的差异

热度:24   发布时间:2024-01-15 16:16:05.0

在Java当中,多线程执行时间是非顺序,这个究竟是怎么回事?

举例代码:

public class ICurrentThread extends Thread {@Overridepublic void run(){super.run();System.out.println("this is my thread");}
}

public class CreateTheadTest {public static void main(String[] args) {System.out.println(Thread.currentThread().getName());ICurrentThread ict=new ICurrentThread();ict.start();System.out.println("run end");}}
运行结果:

main
run end
this is my thread
当在一个进程当中出现两个线程的时候,这两个线程会根据线程调度规则竞争执行时间,顺序将会被打乱。例如示例中main线程中开启了另外一个线程ict,但是ict在运行时慢于main线程,出现了main线程早于ict线程执行完成,虽然main线程中存在执行语句在顺序上慢于ic