prioritydemo.java

来自「精通Java核心技术源代码」· Java 代码 · 共 44 行

JAVA
44
字号
import javax.swing.JOptionPane;
	
	class PriorityDemo 
	{
		public static void main(String args[])
		{
			Priority hpThread,lpThread;
			String output;
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
			hpThread = new Priority(Thread.MAX_PRIORITY-1);
			lpThread = new Priority(Thread.MIN_PRIORITY+2);
			
	      	// 启动线程
			hpThread.start();
			lpThread.start();
			
			try {
				Thread.sleep(5000);
			}
			catch (InterruptedException e) {
				System.out.println(e.getMessage());
			}
			
	   		// 终止线程
			hpThread.stop();
			lpThread.stop();
			
			try {
				hpThread.thread.join();
				lpThread.thread.join();
			}
			catch (InterruptedException e) {
				System.out.println(e.getMessage());
			}
			
			output = "High priority thread has run " + hpThread.runtimes + " times\n"
					 + "Low priority thread has run " + lpThread.runtimes + " times\n";
					 
			JOptionPane.showMessageDialog( null,output );
		 	System.exit( 0 );
		}
	}

⌨️ 快捷键说明

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