首页 新闻 会员 周边

java多线程关于优先级的问题

0
悬赏园豆:50 [待解决问题]

建立两个不同优先级的线程,发现线程建立的顺序,会影响线程抢占CPU的时间,而优先级的设置不起作用,请问这怎么解释?

代码如下:

 1 public class PriorityDemo {
 2     public static void main(String[] args) {
 3         PriThread mt1 = new PriThread("High Priority", Thread.NORM_PRIORITY+2);
 4         PriThread mt2 = new PriThread("Low Priority", Thread.NORM_PRIORITY-2);
 5         mt2.thrd.start();
 6         mt1.thrd.start();
 7         
 8         try {
 9             mt1.thrd.join();//等待线程终止
10             mt2.thrd.join();
11         } catch (InterruptedException e) {
12             System.out.println("Main thread interrupted.");
13         }
14         System.out.println("\nHigh priority thread counted to " + mt1.count);
15         System.out.println("\nLow priority thread counted to " + mt2.count);
16     }
17 }
18 class PriThread implements Runnable {
19 
20     long count;
21     Thread thrd;
22     
23     static boolean stop = false;
24     public PriThread(String name, int pri) {
25         thrd = new Thread(this, name);
26         thrd.setPriority(pri);
27         count = 0;
28         //thrd.start();
29     }
30     @Override
31     public void run() {
32         do {
33             count++;
34             if ((count % 10000) == 0) {
35                 if(thrd.getPriority() > Thread.NORM_PRIORITY) {
36                     //Thread.yield();
37                     count = count;
38                 }
39             }
40         } while (stop == false && count < 100000000);
41         stop = true;
42     }
43 }

运行结果为:

High priority thread counted to 100000000

Low priority thread counted to 75062915

但是交换3,4行的顺序,结果为:

High priority thread counted to 74776252

Low priority thread counted to 100000000
这是为什么呢?

soul390的主页 soul390 | 初学一级 | 园豆:154
提问于:2014-05-15 10:43
< >
分享
所有回答(2)
0

由于你的程序中stop声明的是static boolean类型,

这导致在while判断

while (stop == false && count < 100000000);

stop的变量不是最后修改后的变量, 需要将stop声明为:

static volatile boolean stop = false;

不过改过以后出现了说到的问题了。

我猜测这引起的原因应该是对共享的变量stop调用产生的, 因此对程序进行了修改, 如下:

public class PriorityDemo {
    public static void main(String[] args) {
        PriThread mt1 = new PriThread("High Priority", Thread.NORM_PRIORITY+2);
        PriThread mt2 = new PriThread("Low Priority", Thread.NORM_PRIORITY-2);

        mt1.thrd.start();
        mt2.thrd.start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted.");
        }

        mt1.stop = true;
        mt2.stop = true;

        try {
            mt1.thrd.join();//等待线程终止
            mt2.thrd.join();
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted.");
        }

        System.out.println("\nHigh priority thread counted to " + mt1.count);
        System.out.println("\nLow priority thread counted to " + mt2.count);
    }
}
class PriThread implements Runnable {
 
    long count;
    Thread thrd;
     
    volatile boolean stop = false;

    public PriThread(String name, int pri) {
        thrd = new Thread(this, name);
        thrd.setPriority(pri);
        count = 0;
        //thrd.start();
    }

    public void run() {
        do {
            count++;
        } while (stop == false && count < 100000000);
    }
}

发现, 此时将不会出现你所说的现象。

更深入的机制也不是很清除, 希望对你有所帮助。

grassofsky | 园豆:342 (菜鸟二级) | 2014-05-15 22:29

谢谢你的回答。你的方法也可以验证优先级的作用,我就是不明白为什么会出现我的得出的那种问题呢。

支持(0) 反对(0) soul390 | 园豆:154 (初学一级) | 2014-05-16 21:18
0

优先级只是一种理想情况下  没有说优先级高的一定先处理 多线程  是非常复杂的东西

 

不要在意这些细节

ruoyu4game | 园豆:342 (菜鸟二级) | 2014-05-22 01:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册