在 Java 6 中,为什么即使低优先级线程产生,高优先级线程也不运行? [英] In Java 6, why doesn't higher priority thread not run even if lower priority thread yields?

查看:51
本文介绍了在 Java 6 中,为什么即使低优先级线程产生,高优先级线程也不运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图通过产生较低优先级的线程来运行较高优先级的线程.但它似乎不起作用,高优先级线程似乎在低优先级线程完成后运行.谁能解释一下我做错了什么?

import java.util.ArrayList;导入 java.util.List;公共类 TestThreadPriority 扩展线程 {静态列表<字符串>消息 = 新的 ArrayList();公共无效运行(){线程 t = Thread.currentThread();int 优先级 = t.getPriority();字符串名称 = t.getName();消息.添加(名称+:"+优先级);线程.yield();消息.添加(名称+:"+优先级);}公共静态无效主(字符串 [] args){//TODO 自动生成的方法存根线程 t = Thread.currentThread();t.setPriority(MIN_PRIORITY);int 优先级 = t.getPriority();字符串名称 = t.getName();消息.添加(名称+:"+优先级);线程 tp1 = new TestThreadPriority();tp1.setPriority(MIN_PRIORITY);线程 tp2 = new TestThreadPriority();tp2.setPriority(MAX_PRIORITY);tp1.start();tp2.start();线程.yield();消息.添加(名称+:"+优先级);for(int i = 0; i 

输出为:

<块引用>

  1. main:1
  2. Thread-0:1
  3. Thread-0:1
  4. main:1
  5. 线程 1:10
  6. 线程 1:10

非常感谢任何帮助.

谢谢,四边形

解决方案

我猜你的第一个线程在第二个线程开始之前就已经完成了.也许如果您的线程做了一些真正的工作(甚至只是睡了一会儿),您会看到来自两个线程的重叠消息.

您的代码的另一个问题是您从多个线程访问messages,而没有同步.

在尝试打印 messages 的内容之前,您还应该加入您启动的两个线程,以确保您启动的线程已经记录了它们的消息并且它们不会尝试在迭代列表时修改列表以打印它.

一旦您解决了所有这些问题,最后一点是两个您的线程都在让步,而不仅仅是优先级较低的线程.当较高优先级的线程让步时,较低优先级的线程获得一些运行时间是完全合理的.拥有更高的优先级并不会给你带来垄断.在您的简化代码示例中,优先级可能不会对发生的事情产生太大影响 - 主要取决于哪个线程首先命中 yield 语句的机会.

In the code below, I am trying to get the higher priority thread to run by yielding the lower priority thread. But it doesn't seem to work, the higher priority thread seems to run after lower priority thread finishes. Can anyone explain what I am doing wrong?

import java.util.ArrayList;
import java.util.List;

public class TestThreadPriority extends Thread {

    static List<String> messages = new ArrayList<String>();

    public void run() {
        Thread t = Thread.currentThread();
        int priority = t.getPriority();
        String name = t.getName();
        messages.add(name + ":" + priority);        
        Thread.yield();
        messages.add(name + ":" + priority);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Thread t = Thread.currentThread();
        t.setPriority(MIN_PRIORITY);
        int priority = t.getPriority();
        String name = t.getName();
        messages.add(name + ":" + priority);

        Thread tp1 = new TestThreadPriority();
        tp1.setPriority(MIN_PRIORITY);      
        Thread tp2 = new TestThreadPriority();
        tp2.setPriority(MAX_PRIORITY);      

        tp1.start();
        tp2.start();

        Thread.yield();
        messages.add(name + ":" + priority);

        for(int i = 0; i < messages.size(); i++) {
            System.out.println((i+1) + ". " + messages.get(i));
        }
    }
}

Output is:

  1. main:1
  2. Thread-0:1
  3. Thread-0:1
  4. main:1
  5. Thread-1:10
  6. Thread-1:10

Any help is greatly appreciated.

Thanks, Quadir

解决方案

I'd guess that your first thread has finished executing even before the second thread has started. Perhaps if your threads did some real work (or even just slept a bit) you'd see overlapping messages from the two threads.

Another problem with your code is that you are accessing messages from multiple threads without synchronizing.

You also should join with the two threads you've started before trying to print the contents of messages, to ensure that the threads you started have already logged their messages and that they don't try to modify the list while you're iterating over it to print it.

Once you've fixed all this, one final point is that both your threads are yielding, not just the lower priority thread. When the higher priority thread yields, it's perfectly reasonable that the lower priority thread gets some running time. Having a higher priority doesn't give you a monopoly. In your simplified code example the priority probably won't make much difference to what happens - it will be mostly down to chance as to which thread hits the yield statement first.

这篇关于在 Java 6 中,为什么即使低优先级线程产生,高优先级线程也不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆