Java多线程:setPriority() [英] Java multithreading: setPriority()

查看:76
本文介绍了Java多线程:setPriority()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在线阅读有关 Thread setPriority()的信息,

  final void setPriority(int level) 

此处,级别指定了调用线程的新优先级设置.

来源: http://www.java-samples.com/showtutorial.php?tutorialid = 302

我不了解的是 setPriority()如何更改调用线程的优先级:被调用者不应该影响调用它的对象吗?因此,如果我从 main()调用该方法,那么 main()的优先级会改变吗?在我看来,这肯定是违反直觉的.

那是除非我不理解调用线程的意思.

解决方案

方法 setPriority Thread.java setPriority .这样做将设置调用该方法的线程的优先级.

  UsedDefinedThreadReference = new Thread();usedDefinedThreadReference.setPriority(2); 

上面将创建该线程的一个实例,并将其优先级设置为与当前正在执行的线程相等,如果它的主线程默认情况下将其优先级设置为5.

  Thread.currentThread().setPriority(6); 

以上将当前正在执行的线程的优先级设置为6.

  public static void main(String [] args){Thread.currentThread().setPriority(7);//当前正在执行的线程是Main线程,其优先级从默认值5设置为7线程t = new MyThread();//其优先级设置为7,因为当前执行线程的优先级等于7.t.setPriority(3);//t引用的Thread对象的优先级设置为3.Thread.currentThread().setPriority(8);//主线程当前正在执行线程,因此其优先级设置为8.}公共类MyThread扩展了线程{公共线程(){Thread.currentathread().setPriority(9);//主线程当前正在执行线程,因此其优先级设置为9.this.setPriority(8);//这是指MyThread的对象,当前对象,因此其优先级设置为8而不是main.}public void run(){Thread.currentThread().setPriority(2);//当前正在执行的线程是MyThread的对象,因此其优先级设置为2.this.setPriority(4);//当前执行的线程还是MyThread的Object,因此其优先级设置为4.}} 

I was reading online about Thread's setPriority() and came across the following:

final void setPriority(int level)

Here, level specifies the new priority setting for the calling thread.

Source:http://www.java-samples.com/showtutorial.php?tutorialid=302

What I don't understand is how setPriority() alters the priority for the calling thread: shouldn't it affect the object on which it is invoked, the callee? So if I call the method from main(), then main()'s priority will be altered? This sure looks to me as counter-intuitive.. .

That is unless I don't understand what's meant by a calling thread.. .

解决方案

The methid setPriority is defined in Thread.java https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int)

Priority is helpful hint for thread scheduler to decide which thread to give priority while allocating CPU. By default Main thread starts the execution of a java program. Developers can create their custom Thread.java's objects. When these threads are started then the code initiated by the run method of the thread executes in its path of execution of that particular thread. Each thread is backed by a method call stack. The default priority of the Main thread is 5. Any thread created is provided with the priority value same as of the thread in whose execution path it is created.

You need an object on which you can invoke (call) a method upon.

What do you mean when you say "...if I call the method from main(),..." ? You just cant call it without any instance of thread. You can create an instance of a Thread and then invoke the method setPriority on it. Doing this will set the priority of the thread upon which you invoke the method.

Thread usedDefinedThreadReference = new Thread();
usedDefinedThreadReference.setPriority(2);

Above will create an instance of the thread and set its priority equal to the currently executing thread , if its main thread then by default its priority is set to 5.

Thread.currentThread().setPriority(6); 

Above will set the priority of the currently executing thread as 6.

public static void main(String[] args){
     Thread.currentThread().setPriority(7); // currently executing thread is Main thread and its priority is set as 7 from its default value of 5
     Thread t = new MyThread(); // its priority is set to 7 as current executing thread has priority equal to 7.

    t.setPriority(3); // priority of Thread object referenced by t is set to 3.

   Thread.currentThread().setPriority(8); // main thread is currently executing thread hence its priority is set as 8.

}

public class MyThread extends Thread {

     public Thread(){
          Thread.currentathread().setPriority(9); // Main thread is currently executing thread hence its priority is set as 9.
          this.setPriority(8); // this refers to object of MyThread , the current object hence its priority is set as 8 not of main.
     }
     public void run(){
         Thread.currentThread().setPriority(2); // currently executing thread is MyThread`s object hence its priority is set as 2.
         this.setPriority(4); // again currently executing thread is Object of MyThread hence its priority is set as 4.
     }
} 

这篇关于Java多线程:setPriority()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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