java---多线程sleep()和setPriority()方法

package ThreadIO2_1.Thread.schedule.v1;

public class MyRunner extends Thread {
	private static Object obj = new Object();

	public void run() {
		synchronized (obj) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				System.out.println(this.getName() + "已经被唤醒");
			}
			for (int i = 1; i < 101; i++) {
				System.out.println(Thread.currentThread().getName() + "--No.--"
						+ i);
			}
		}
	}

}

package ThreadIO2_1.Thread.schedule.v1;

public class Schedule {
	public static void main(String[] args) {
		Thread t1 = new MyRunner();
		t1.start();
		Thread t2 = new MyRunner();
		t2.start();
		// 采用优先级进行相对调度,相比优先级高的抢占资源的概率要高一些,同样的优先级在前面的调度的更快
		t1.setPriority(9);
		t2.setPriority(3);
		try {
			Thread.sleep(2000);
			// t2.interrupt();// 强制唤醒t2线程;t2也就是运行中的Thread-1,t1是运行中的Thread-0;
		} catch (InterruptedException e) {
			System.out.println("~~~");
		}

	}
}

你可能感兴趣的:(java---多线程sleep()和setPriority()方法)