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

📄 hilopri.java

📁 java 数据结构的一些简单实例和一些线程的应用主要是面向初学者
💻 JAVA
字号:
class Clicker implements Runnable
{
	int click=0;
	Thread t;
	private volatile boolean running=true;
	
	public Clicker(int p)
	{
		t=new Thread(this);
		t.setPriority(p);
	}
	
	public void run()
	{
		while(running)
		{
			click++;
		}
	}
	
	public void stop()
	{
		running=false;
	}
	
	public void start()
	{
		t.start();
	}
	
}

public class HiLoPri 
{
     public static void main(String[] args)
     {
    	 Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    	 Clicker hi=new Clicker(Thread.NORM_PRIORITY+2);
    	 Clicker lo=new Clicker(Thread.NORM_PRIORITY-2);
    	 
    	 lo.start();
    	 hi.start();
    	 
    	 try
    	 {
    		 Thread.sleep(10000);
    	 }
    	 catch(InterruptedException e)
    	 {
    		 System.out.println("Main thread interrupted.");
    	 }
    	 
    	 hi.stop();
    	 lo.stop();
    	 
    	 try
    	 {
    		 hi.t.join();
    		 lo.t.join();
    	 }
    	 catch(InterruptedException e)
    	 {
    		 System.out.println("InterruptedException caught");
    	 }
    	 System.out.println("Low-priority thread:"+lo.click);
    	 System.out.println("High-priority thread:"+hi.click);
     }
}

⌨️ 快捷键说明

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