读书人

为毛小弟我每次运行都是一样的结果?求

发布时间: 2013-10-24 18:27:21 作者: rapoo

为毛我每次运行都是一样的结果?求大神指点迷津//关于多线程的例子

//【例7.1】  声明继承Thread类的奇数/偶数序列线程。

public class NumberThread extends Thread
{
private int first; //序列初值

public NumberThread(String name,int first)
{
super(name);
this.first = first;
}

public NumberThread(String name)
{
this(name,0);
}

public void run() //线程体,覆盖Thread的run()
{
System.out.print("\n"+this.getName()+": ");
for (int i=first; i<50; i+=2)
System.out.print(i+" ");
System.out.println(this.getName() +"结束!");
}

public static void main(String args[])
{
System.out.println("currentThread="+Thread.currentThread().getName()); //输出当前线程对象名
System.out.println("main Priority="+Thread.currentThread().getPriority()); //输出当前线程对象的优先级

NumberThread thread_odd = new NumberThread("奇数线程",1); //创建线程对象
NumberThread thread_even = new NumberThread("偶数线程",2);
//thread_even.setPriority(10); //设置优先级10为最高,1为最低

thread_odd.start(); //启动线程对象
thread_even.start();
System.out.println("activeCount="+Thread.activeCount());//输出当前活动线程数
}
}

/*
程序运行结果如下:
currentThread=main
activeCount=3
main Priority=5

奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27
偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 偶数线程结束!
29 31 33 35 37 39 41 43 45 47 49 奇数线程结束!




再次运行
thread_odd.setPriority(10); //最高优先级

奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37
39 41 43 45 47 49 奇数线程结束!
activeCount=2

偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 偶数线程结束!




再次运行
thread_odd.setPriority(1); //最低优先级

activeCount=3
偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 偶数线程结束!

奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37
39 41 43 45 47 49 奇数线程结束!

*/
多线程 thread


[解决办法]
我感觉,不论设置什么优先级,
thread_odd.start(); 这个执行的时候貌似都没有出现抢占CPU的现象
[解决办法]
还有个问题要注意,多核和单核效果是不一样的。一般单核能比较好调试的看出来

读书人网 >Eclipse开发

热点推荐