线程优先权举例 代码感觉没问题 但是运行出来没体现线程的优先权

六道对穿肠 2012-10-13 10:09:55
public class TestPriority// 线程的优先权 举例 未成功
{
public static void main(String[] args)
{
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3);//增加优先级
t1.start();
t2.start();
}

}

class T1 implements Runnable
{
public void run()
{
for (int i = 0; i < 1000; i++)
{
System.out.println("T1:" + i);
}
}
}

class T2 implements Runnable
{
public void run()
{
for (int i = 0; i < 1000; i++)
{
System.out.println("---------T2:" + i);
}
}
}
...全文
409 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
GS2012 2013-05-11
  • 打赏
  • 举报
回复
1楼是大神,受教了
JPF1024 2013-05-11
  • 打赏
  • 举报
回复
引用 9 楼 sxh53 的回复:
学习了。 TestPriority.class.wait(); 这句话也是为了同步而用的吗?
不是为了同步而用的,一般同步使用synchronized也有使用lock对象. 这句话是等待当前线程完成。 资料: wait public final void wait() throws InterruptedException 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。 当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。 对于某一个参数的版本,实现中断和虚假唤醒是可能的,而且此方法应始终在循环中使用: synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition } 此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。 抛出: IllegalMonitorStateException - 如果当前线程不是此对象监视器的所有者。 InterruptedException - 如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的 中断状态 被清除。 另请参见: notify(), notifyAll()
JPF1024 2013-05-11
  • 打赏
  • 举报
回复
引用 1 楼 ldh911 的回复:
首先:线程优先级的作用不是绝对意义上的,而是概率上的,可以多运行几次; 其次:你的程序没有真正意义上同步开始,那么先执行了start的会有优势。 稍微修改了下:

public class TestPriority {
    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(new PriorityT1());
        Thread t2 = new Thread(new PriorityT2());
        t1.setPriority(Thread.NORM_PRIORITY + 3);//增加优先级
        t1.start();
        t2.start();
        Thread.sleep(100);
        synchronized (TestPriority.class) {
            TestPriority.class.notifyAll();
        }
    }

}

class PriorityT1 implements Runnable {
    public void run() {
        synchronized (TestPriority.class) {
            try {
                TestPriority.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 1000; i++) {
            System.out.println("T1:" + i);
        }
    }
}

class PriorityT2 implements Runnable {
    public void run() {
        synchronized (TestPriority.class) {
            try {
                TestPriority.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 1000; i++) {
            System.out.println("--T2:" + i);
        }
    }
}
说得很对。线程的运行是不可控制的,特别是设置线程的优先级,并且通常推荐使用JAVA定义好的静态常量,以便兼容所有平台。
sxh53 2013-05-11
  • 打赏
  • 举报
回复
学习了。 TestPriority.class.wait(); 这句话也是为了同步而用的吗?
zhaogang 2013-05-10
  • 打赏
  • 举报
回复
楼主,先支持你代码上的一点问题 synchronized (TestPriority.class) 如果你的方法不是static,只要写synchronized (this)就行了 回答你的问题,Java的线程的线程优先级分10级,但Windows的线程优先级只有7级,如果你是在Windows上跑的话,就是所有部分级别会被合并,再加上,Java中优先级高的线程并不代表就比优先级低的线程完全优先,只能是可能是优先执行,所以不要想完全靠优先级来解决线程的执行顺序问题。 还有,楼主的代码中,写的是两个线程类,并无形成线程竞争,所以也无法体现出哪个优先。
javamy004 2013-05-10
  • 打赏
  • 举报
回复
我只是来围观一下!!
constanine_xia 2013-05-10
  • 打赏
  • 举报
回复
优先级只是谁先运行,顶2楼大神,如果不是同步而是并发的话,那么就无法确定谁先完成。
aimsgmiss 2012-10-14
  • 打赏
  • 举报
回复
楼上的是为大神级别的
MiceRice 2012-10-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
TestPriority.class 这句话什么意思
[/Quote]

只是借用来作为同步的对象而已,也可以用别的,但必须是两个线程均可访问到的,比如可以是某静态成员变量。
licip 2012-10-14
  • 打赏
  • 举报
回复
这个同问:
synchronized (TestPriority.class) {
aimsgmiss 2012-10-14
  • 打赏
  • 举报
回复
TestPriority.class 这句话什么意思
MiceRice 2012-10-13
  • 打赏
  • 举报
回复
首先:线程优先级的作用不是绝对意义上的,而是概率上的,可以多运行几次;
其次:你的程序没有真正意义上同步开始,那么先执行了start的会有优势。

稍微修改了下:

public class TestPriority {
public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new PriorityT1());
Thread t2 = new Thread(new PriorityT2());
t1.setPriority(Thread.NORM_PRIORITY + 3);//增加优先级
t1.start();
t2.start();
Thread.sleep(100);
synchronized (TestPriority.class) {
TestPriority.class.notifyAll();
}
}

}

class PriorityT1 implements Runnable {
public void run() {
synchronized (TestPriority.class) {
try {
TestPriority.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 1000; i++) {
System.out.println("T1:" + i);
}
}
}

class PriorityT2 implements Runnable {
public void run() {
synchronized (TestPriority.class) {
try {
TestPriority.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 1000; i++) {
System.out.println("--T2:" + i);
}
}
}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧