java线程优先级
Ⅰ java中如何做线程的优先级
{
publicMyMoreThread()
{
super();
}
publicMyMoreThread(Stringname)
{
super(name);
}
publicvoidrun()
{
for(inti=0;i<10;i++)
{
System.out.println(super.getName()+":"+i);
}
}
}
publicclassPriorityTest
{
publicstaticvoidmain(String[]args)
{
MyMoreThreadthread1=newMyMoreThread("第1个线程");
MyMoreThreadthread2=newMyMoreThread("第2个线程");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
Ⅱ java线程优先级有什么意义
》》。。。而sleep之后就会随机排序了,优先级此时就不起作用了,有没有解决的办法?
其中一个办法是让每一个线程记得必须比它先完成的另一个线程,
然后在必要时强制它放弃执行一直到那另一个线程执行完为止。 比如:
class CustomThread extends Thread {
private Thread 先完;
CustomThread(String name, Thread 先完) {
super(name);
this.先完 = 先完;
}
public void run() {
System.out.println(this.getName());
try {
sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (先完 != null)
try {
先完.join();
} catch (InterruptedException e) {}
System.out.println("NAME: " + this.getName() + " Bye");
}
}
class MultiThread {
public static void main(String args[]) throws InterruptedException {
CustomThread thread1 = new CustomThread("first",null);
CustomThread thread2 = new CustomThread("second",thread1);
CustomThread thread3 = new CustomThread("third",thread2);
CustomThread thread4 = new CustomThread("fourth",thread3);
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY-1);
thread3.setPriority(Thread.MAX_PRIORITY-2);
thread4.setPriority(Thread.MAX_PRIORITY-3);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
4 等 3、3 等 2、。。。 串联成单一线程了,所以如果情况允许的话,
把所有必须顺序执行的任务集合在单一线程里(比如留在 main 里)是另一个办法。
》》。。。优先级到底有什么用处?
轻易地让程序即使在非常忙碌的时候也能迅速处理使用者的输入(该输入也许是调整某项操作的优先级:)、编写游戏时更方便地实现战场上级别较高的一方优先攻击、等等。
Ⅲ java 如何设置线程的优先级
{
MyThread1(Stringname){
super(name);
}
@Override
publicvoidrun(){
for(inti=0;i<10000;i++){
System.out.println(getName()+":"+i);
}
}
}
{
MyThread2(Stringname){
super(name);
}
@Override
publicvoidrun(){
for(inti=0;i<10000;i++){
System.out.println(getName()+":"+i);
}
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
MyThread1t1=newMyThread1("t1");
MyThread1t2=newMyThread1("t2");
t1.setPriority(Thread.NORM_PRIORITY+3);
t1.start();
t2.start();
}
}
Ⅳ Java如何获得运行线程的优先级
package com.yii;public class GettingThreadPriority extends Thread {
private int countDown = 5;
private volatile double d = 0;
public GettingThreadPriority(int priority) {
setPriority(priority);
start();
}
public String toString() {
return super.toString() + ": " + countDown;
}
public void run() {
while (true) {
for (int i = 1; i < 100000; i++)
d = d + (Math.PI + Math.E) / (double) i;
System.out.println(this);
if (--countDown == 0)
return;
}
}
public static void main(String[] args) {
new GettingThreadPriority(Thread.MAX_PRIORITY);
for (int i = 0; i < 5; i++) {
new GettingThreadPriority(Thread.MIN_PRIORITY);
}
}}
Ⅳ java 里面,主线程的优先级可以设置吗
如果你是要主线程高于子线程的话 .
个人理解 因为我刚学 .直接改主线程的对象 然后 改子线程 优先级
public class ThreadDemo extends Thread{
public void run() {
System.out.println(currentThread().getName()+ "-" +"优先级为:"+currentThread().getPriority());
}
public static void main(String [] args) {
Thread t1 = new Thread();
Thread t = new Thread( new ThreadDemo()); //为ThreadDemo()的子线程
t.setPriority(10); //当前子线程对象 优先级10
Thread.currentThread().setPriority(1); //当前主线程为优先为1
t.start(); //输出当前子线程
t1.start(). //输出主线程
}
上次我的理解有误,特地来来补充
直接声明 Thread 对象就是主线程
如果向设置主线程的优先级
可以直接
Thread.currentThread().setPriority(线程优先级);
就是当前主线程的优先级
如果想要在控制台输出的话 可以直接
getPriority //get你要输出的线程
Ⅵ java线程中的优先级问题
楼主请记住一点,多线程的执行本身就是多个线程的交换执行,并非同时执行,执行的优先级只是他执行的概率。
例如原本优先级一样,那么两个线程的执行的概率都为50%。现在我们提高其中一个,那么一个为60%的概率抢到进入CPU执行的机会,另一个是依旧是40%。执行完一次之后又要重新抢占CPU。但是40%概率进入的线程也有可能抢到,虽然概率低了点,但总会有他执行的机会,万一次次都抽中40%的概率呢?所以你要正确理解线程和线程之间的优先级
Ⅶ JAVA多线程优先级运行顺序的问题
这个。。你不必纠结。。
深入说的话,其实。。很深。。
首先说 线程优先级,并不能保证优先级高的先运行,也不保证优先级高的更多的分配CPU时间,只是对系统的建议而已,到底运行哪个,是操作系统决定的,都不是java说了算的。
另外java只能保证在线程内部看起来是顺序执行你的代码的,并不能保证从其他线程看来这个是按照你编码顺序执行的。。