Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程优先级无法正常工作。我在代码中做错了什么?_Java_Multithreading - Fatal编程技术网

Java 线程优先级无法正常工作。我在代码中做错了什么?

Java 线程优先级无法正常工作。我在代码中做错了什么?,java,multithreading,Java,Multithreading,因此,我创建了两个类,MyThread1和MyThread2,它们扩展了线程类 public class MyThread1 extends Thread{ public void run() { System.out.println("MyThread1 output"); } } public class MyThread2 extends Thread{ public void run() { System.out.printl

因此,我创建了两个类,
MyThread1
MyThread2
,它们扩展了线程类

public class MyThread1 extends Thread{

    public void run() {
        System.out.println("MyThread1 output");
    }
}

public class MyThread2 extends Thread{

    public void run() {
        System.out.println("MyThread2 output");
    }
}

public class Main {

    public static void main(String[] args) {

        MyThread1 mt1 = new MyThread1();
        MyThread2 mt2 = new MyThread2();

        mt1.setPriority(10);

        mt1.start();
        mt2.start();
        System.out.println("aaaaaa");
    }
}
Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
# 现在根据我的程序
MyThread1
应该在
MyThread2
之前运行,因为我已经将其优先级设置为10,但我仍然得到以下输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
#
请有人告诉我为什么
Mythread2
先打印,然后
MyThread1
甚至认为
MyThread1
具有高优先级。

关于您的问题,我想您在执行之前不会保存代码。确保它已保存并再次执行

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
创建两个线程1和2时,它们的优先级从主线程继承。假设主线程的优先级为5,则优先级为5。 当您将线程1的优先级设置为10(最大优先级)时。结果应该是:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
Thread 1....
Thread 2 or Main thread ( because of same priority)
我已附上我的样本代码:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
     Runnable r1 = new Runnable() {
        public void run() {
            System.out.println("Runnable 1");
        }
    };
    Runnable r2 = new Runnable() {
        public void run() {
            System.out.println("Runnable 2");
        }
    };

    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);
    t2.setPriority(Thread.MAX_PRIORITY);
    t1.start();
    t2.start();

    System.out.println("Main thread");
输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
Runnable 2
Runnable 1
Main thread

关于您的问题,我想您在执行之前不会保存代码。确保它已保存并再次执行

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
创建两个线程1和2时,它们的优先级从主线程继承。假设主线程的优先级为5,则优先级为5。 当您将线程1的优先级设置为10(最大优先级)时。结果应该是:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
Thread 1....
Thread 2 or Main thread ( because of same priority)
我已附上我的样本代码:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
     Runnable r1 = new Runnable() {
        public void run() {
            System.out.println("Runnable 1");
        }
    };
    Runnable r2 = new Runnable() {
        public void run() {
            System.out.println("Runnable 2");
        }
    };

    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);
    t2.setPriority(Thread.MAX_PRIORITY);
    t1.start();
    t2.start();

    System.out.println("Main thread");
输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
Runnable 2
Runnable 1
Main thread

只需在@htpvl上添加,

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
在Java中,线程有自己的int类型变量作为优先级值 as(
MAX\u PRIORITY,NORM\u PRIORITY,MIN\u PRIORITY

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
正如@htpvl所解释的,您可以利用它

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
您还可以围绕其值进行调整,如增加或减少。
例如:假设您有5个线程,并且您希望相应地分配优先级,那么您可以做的是:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
// Creating instances
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);
Thread t4 = new Thread(r4);
Thread t5 = new Thread(r5);

// Do check the value of Thread.MAX_PRIORITY, and you can play around it
System.out.println("MAX PRIORITY IS: " + Thread.MAX_PRIORITY); // On Mac it prints : 10
System.out.println("NORMAL PRIORITY IS: " + Thread.NORM_PRIORITY); // On Mac it prints : 5
System.out.println("MIN PRIORITY IS: " + Thread.MIN_PRIORITY); // On Mac it prints : 1

// Set the priority (according to your PRIORITY int values)
t1.setPriority((int)Thread.MAX_PRIORITY-1);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t4.setPriority((int)Thread.NORM_PRIORITY +1);
t5.setPriority((int)Thread.NORM_PRIORITY +2);

// Starting the threads [Execution]
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
线程序列的输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
t2
t1
t5
t4
t3

只需在@htpvl上添加,

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
在Java中,线程有自己的int类型变量作为优先级值 as(
MAX\u PRIORITY,NORM\u PRIORITY,MIN\u PRIORITY

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
正如@htpvl所解释的,您可以利用它

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
您还可以围绕其值进行调整,如增加或减少。
例如:假设您有5个线程,并且您希望相应地分配优先级,那么您可以做的是:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
// Creating instances
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);
Thread t4 = new Thread(r4);
Thread t5 = new Thread(r5);

// Do check the value of Thread.MAX_PRIORITY, and you can play around it
System.out.println("MAX PRIORITY IS: " + Thread.MAX_PRIORITY); // On Mac it prints : 10
System.out.println("NORMAL PRIORITY IS: " + Thread.NORM_PRIORITY); // On Mac it prints : 5
System.out.println("MIN PRIORITY IS: " + Thread.MIN_PRIORITY); // On Mac it prints : 1

// Set the priority (according to your PRIORITY int values)
t1.setPriority((int)Thread.MAX_PRIORITY-1);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t4.setPriority((int)Thread.NORM_PRIORITY +1);
t5.setPriority((int)Thread.NORM_PRIORITY +2);

// Starting the threads [Execution]
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
线程序列的输出:

Main thread output.                                                      
MyThread2 output.                                                             
MyThread1 output.   
t2
t1
t5
t4
t3

这是
java
?@Cid是兄弟。你知道答案吗?我不认为这会测试优先级。任务非常小,线程可能没有任何争用。如果你大量运行它,有时1是第一,有时2是第一。这是
java
?@Cid yes bro。你知道答案吗?我不认为这会测试优先级。任务非常小,线程可能没有任何争用。如果你大量运行它,有时1是第一,有时2是第一。谢谢你的回复,伙计。但我做了一些研究,发现windows10不支持多线程。是真的吗?谢谢你的回复,老兄。但我做了一些研究,发现windows10不支持多线程。这是真的吗?