[code]
class test extends Thread
{
public test (String str)
{
super(str);
}
public void run()
{
for(int i=0;i<20;i++){
System.out.println(getName());
}
}
}public class ThreadTest {
public static void main(String []args)
{
test t1=new test("Thread1");
test t2=new test("Thread2");
test t3=new test("Thread3");
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
t1.start();
t2.start();
t3.start();
}
}[/code]
输出结果是不确定的,其中如下是一种:
Thread1
Thread2
Thread1
Thread2
Thread1
Thread1
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread1
Thread1
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
这是个关于线程优先级的程序,为何线程不是按照优先级由高到底的顺序执行呢?
如果将线程优先级设置为相同的,那两种情况输出结果会有什么区别?
class test extends Thread
{
public test (String str)
{
super(str);
}
public void run()
{
for(int i=0;i<20;i++){
System.out.println(getName());
}
}
}public class ThreadTest {
public static void main(String []args)
{
test t1=new test("Thread1");
test t2=new test("Thread2");
test t3=new test("Thread3");
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
t1.start();
t2.start();
t3.start();
}
}[/code]
输出结果是不确定的,其中如下是一种:
Thread1
Thread2
Thread1
Thread2
Thread1
Thread1
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread2
Thread1
Thread1
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread3
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
这是个关于线程优先级的程序,为何线程不是按照优先级由高到底的顺序执行呢?
如果将线程优先级设置为相同的,那两种情况输出结果会有什么区别?
解决方案 »
- MyEclipse 7.5如何汉化
- java使用Calendar的怪异问题,高手请进.
- 好心人帮帮我,用Java模拟HTTP Request上传Excel文件。
- 我是一个初学者,请问如何配置java的运行环境!!
- 向大家推荐一个不错的JAVA论坛,功能强大,送分功能更完善
- 散分,applet中出现java.lang.NoSuchMethodError: main
- 高手请近,applet的问题
- 关于Clob的用法
- Exception in thread "main" java.lang.NoClassDefFoundError: hello/java
- Exception in thread "main" java.lang.NoClassDefFoundError: xunhuan
- 关于 java.lang.OutOfMemoryError : Java heap space 的问题
- 怎么设置默认按钮???
如果优先级相同,那么就取决于CPU了,他会在空闲时间时执行其它线程.打印结果更是杂乱无章