• 多线程学习(三)


    多线程的一些其他操作:

    sleep

    public class SleepingTask implements Runnable {
    
    	private int countDown = 100;
    	private static int taskCount = 0;
    	private final int id = taskCount++;
    
    	public String status() {
    		return "#" + id + "(" + (countDown > 0 ? countDown : "lift off") + ")";
    	}
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while (countDown-- > 0) {
    			System.out.println(status());
    
    			try {
    				// old style
    //				Thread.sleep(100);
    				//JavaSE 5/6 style
    				TimeUnit.MILLISECONDS.sleep(100);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    				System.err.println("interrupted!");
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		ExecutorService service = Executors.newCachedThreadPool();
    		SleepingTask sleepingTask = new SleepingTask();
    		for (int i = 0; i < 10; i++) {
    			//线程不安全的
    			service.execute(sleepingTask);
    		}
    	}
    }
    

    线程阻塞 使得任务调度器可以切换到另一个线程,进而可以驱动另一个任务。但是也的看底层系统实现

    优先级

    调度器倾向于让优先级更高的线程先执行,绝大数情况下线程以默认的优先级执行。

    
    public class SimplePriority implements Runnable {
    	private int priority;
    	private int countDown=5;
    
    	public SimplePriority(int priority) {
    		super();
    		this.priority = priority;
    	}
    
    	@Override
    	public String toString() {
    		return Thread.currentThread().getName() + " priority:" + priority + " countDown:" + countDown;
    	}
    
    	@Override
    	public void run() {
    		Thread.currentThread().setPriority(priority);
    		while (true) {
    			for (int i = 0; i < 1000; i++) {
    				@SuppressWarnings("unused")
    				double a = (Math.PI + Math.E) / i;// 耗时操作
    				if (i % 100 == 0) {
    					Thread.yield();
    				}
    			}
    			System.out.println(this);
    			if (--countDown == 0)
    				return;
    		}
    	}
    
    	public static void main(String[] args) {
    		ExecutorService service = Executors.newCachedThreadPool();
    		for(int i=0;i<5;i++){
    			service.execute(new SimplePriority(Thread.MIN_PRIORITY));
    		}
    		service.execute(new SimplePriority(Thread.MAX_PRIORITY));
    		service.shutdown();
    	}
    }
    

    结果:

    pool-1-thread-1 priority:1 countDown:5
    pool-1-thread-6 priority:10 countDown:5
    pool-1-thread-4 priority:1 countDown:5
    pool-1-thread-5 priority:1 countDown:5
    pool-1-thread-3 priority:1 countDown:5
    pool-1-thread-2 priority:1 countDown:5
    pool-1-thread-5 priority:1 countDown:4
    pool-1-thread-4 priority:1 countDown:4
    pool-1-thread-6 priority:10 countDown:4
    pool-1-thread-1 priority:1 countDown:4
    pool-1-thread-6 priority:10 countDown:3
    pool-1-thread-1 priority:1 countDown:3
    pool-1-thread-6 priority:10 countDown:2
    pool-1-thread-4 priority:1 countDown:3
    pool-1-thread-5 priority:1 countDown:3
    pool-1-thread-6 priority:10 countDown:1
    pool-1-thread-2 priority:1 countDown:4
    pool-1-thread-3 priority:1 countDown:4
    pool-1-thread-2 priority:1 countDown:3
    pool-1-thread-5 priority:1 countDown:2
    pool-1-thread-4 priority:1 countDown:2
    pool-1-thread-1 priority:1 countDown:2
    pool-1-thread-5 priority:1 countDown:1
    pool-1-thread-1 priority:1 countDown:1
    pool-1-thread-2 priority:1 countDown:2
    pool-1-thread-3 priority:1 countDown:3
    pool-1-thread-2 priority:1 countDown:1
    pool-1-thread-3 priority:1 countDown:2
    pool-1-thread-3 priority:1 countDown:1
    pool-1-thread-4 priority:1 countDown:1
    

    可以看出优先级高的话执行的频率更高

  • 相关阅读:
    android如何在代码中设置margin
    也许游戏 它P/N图分析
    【淡墨Unity3D Shader计划】四 热带雨林的文章: 排除、深度测试、Alpha测试和基本雾编译
    HDU 3060 多边形面积并
    onmouseover和onmouseout的烦恼
    LoaderManager使用具体解释(三)---实现Loaders
    [每天一个Linux小技巧] gdb 下一次运行多个命令
    VB.NET版机房收费系统—DataGridView应用
    数据结构导论第一遍
    轻松搞定面试中的二叉树题目
  • 原文地址:https://www.cnblogs.com/joeCqupt/p/6814333.html
Copyright © 2020-2023  润新知