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);
    }
    }
}

解决方案 »

  1.   

    首先:线程优先级的作用不是绝对意义上的,而是概率上的,可以多运行几次;
    其次:你的程序没有真正意义上同步开始,那么先执行了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);
            }
        }
    }
      

  2.   

    TestPriority.class 这句话什么意思
      

  3.   

    这个同问:
    synchronized (TestPriority.class) {
      

  4.   

    楼主,先支持你代码上的一点问题
    synchronized (TestPriority.class)
    如果你的方法不是static,只要写synchronized (this)就行了回答你的问题,Java的线程的线程优先级分10级,但Windows的线程优先级只有7级,如果你是在Windows上跑的话,就是所有部分级别会被合并,再加上,Java中优先级高的线程并不代表就比优先级低的线程完全优先,只能是可能是优先执行,所以不要想完全靠优先级来解决线程的执行顺序问题。还有,楼主的代码中,写的是两个线程类,并无形成线程竞争,所以也无法体现出哪个优先。
      

  5.   

    学习了。
    TestPriority.class.wait();
    这句话也是为了同步而用的吗?
      

  6.   


    说得很对。线程的运行是不可控制的,特别是设置线程的优先级,并且通常推荐使用JAVA定义好的静态常量,以便兼容所有平台。
      

  7.   


    不是为了同步而用的,一般同步使用synchronized也有使用lock对象.
    这句话是等待当前线程完成。资料:waitpublic 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()