zoukankan      html  css  js  c++  java
  • java多线程设置优先级

    package com.itbuluoge.mythread;
    
    class SimpleThread extends Thread
    {
    	private int priority;
    	public SimpleThread(int i)
    	{
    		priority=i;
    	}
    	public void run()
    	{
    		Thread.currentThread().setPriority(priority);
    		for(int i=0;i<10000;i++)
    		{
    			try {
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    			System.out.println(priority);
    		}
    	}
    }
    public class PriorityDemo {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SimpleThread high=new SimpleThread(Thread.MAX_PRIORITY);
    		SimpleThread low=new SimpleThread(Thread.MIN_PRIORITY);
    		
    		high.start();
    		low.start();
    	}
    
    }

    如上述代码所看到的,执行的两个线程,分别设置最高和最低的优先级,我们能够看看输出结果:

    10
    1
    10
    1
    10
    1
    10
    1
    10
    1
    10
    1
    10
    1
    10
    1
    1
    10
    1
    10
    10
    1
    10
    1
    1


    能够初步发现。优先级高的,得到的调度会更优先一些。可是也不是很明显。

  • 相关阅读:
    读取radio的value值
    Bootstrap初学(一)
    移动测试用例
    Python 打包成exe执行文件
    Python 模块导入
    Sublime Text2编辑器
    发送Email
    读写TXT文档
    JS与Jquery
    自动化测试摸索
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6905653.html
Copyright © 2011-2022 走看看