在书中看到的大致代码如下
//创建线程类Thread1
//线程体
public void run() { try { System.out.println(getName()+" sleep ");
this.sleep(1000); } catch(InterruptedException e) { System.out.println(e.getMessage()); } System.out.println(getName+"end"); } //创建线程实例A,B,C
public static void main(String[] args)
{
(new Thread1("A")).start();
(new Thread1("B")).start();
(new Thread1("C")).start();
}
书上的输出结果是:A sleep B sleep C sleep A end B end C end
我的理解是:当线程A休眠时,就跳出线程体;调用线程B,依此下去。当休眠过后,再调用线程时执行线程体得到 A end B end C end。难道是执行跳出时以后的部分?
不知道有没有说明白,就是想知道结果 A end B end C end 是怎么得来的?
//创建线程类Thread1
//线程体
public void run() { try { System.out.println(getName()+" sleep ");
this.sleep(1000); } catch(InterruptedException e) { System.out.println(e.getMessage()); } System.out.println(getName+"end"); } //创建线程实例A,B,C
public static void main(String[] args)
{
(new Thread1("A")).start();
(new Thread1("B")).start();
(new Thread1("C")).start();
}
书上的输出结果是:A sleep B sleep C sleep A end B end C end
我的理解是:当线程A休眠时,就跳出线程体;调用线程B,依此下去。当休眠过后,再调用线程时执行线程体得到 A end B end C end。难道是执行跳出时以后的部分?
不知道有没有说明白,就是想知道结果 A end B end C end 是怎么得来的?
你可以试一下,把ThreadC的优先级设高,这样就可能会出现C end A end B end的结果.