線程優先級問題

//Java線程優先級範圍1-10  默認爲5  在cpu同一時間片內 優先級大的搶佔cpu優點大   

public class Test {
    public static void main(String[] args) {
        Thread t1 = new MyThread1();
        Thread t2 = new Thread(new MyRunnable());
        t1.setPriority(10);
        t2.setPriority(1);

        t2.start();
        t1.start();
    }
}

class MyThread1 extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("線程1第" + i + "次執行!");
        }
    }
}

class MyRunnable implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("線程2第" + i + "次執行!");
        }
    }
}
相關文章
相關標籤/搜索