⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setpriority.java

📁 java多线程编程实例_Source
💻 JAVA
字号:
public class SetPriority extends Object {
	private static Runnable makeRunnable() {
		Runnable r =  new Runnable() {
				public void run() {
					for ( int i = 0; i < 5; i++ ) {
						Thread t = Thread.currentThread();
						System.out.println(
							"in run() - priority=" + 
							t.getPriority() +
							", name=" + t.getName());

						try {
							Thread.sleep(2000);
						} catch ( InterruptedException x ) {
							// ignore
						}
					}
				}
			};

		return r;
	}

	public static void main(String[] args) {
		Thread threadA = new Thread(makeRunnable(), "threadA");
		threadA.setPriority(8);
		threadA.start();

		Thread threadB = new Thread(makeRunnable(), "threadB");
		threadB.setPriority(2);
		threadB.start();

		Runnable r = new Runnable() {
				public void run() {
					Thread threadC = 
						new Thread(makeRunnable(), "threadC");
					threadC.start();
				}
			};
		Thread threadD = new Thread(r, "threadD");
		threadD.setPriority(7);
		threadD.start();

		try { Thread.sleep(3000); } 
		catch ( InterruptedException x ) { }

		threadA.setPriority(3);
		System.out.println("in main() - threadA.getPriority()=" +
				threadA.getPriority());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -