package cn.zyw.thread; /** * Created by zyw on 2016/4/3. */ public class MainTest { public static void main(String args[]){ RunnableTest runnable=new RunnableTest("aa"); Thread thread=new Thread(runnable); thread.setPriority(Thread.MAX_PRIORITY);//as if it didn't work. thread.start(); Thread.yield();//线程让步 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(">>>>>>>>>>."); System.out.println(Thread.currentThread().getPriority());//mian priority=5=Thread.NORM_PRIORITY try { thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("............"); try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } runnable.stop(); System.out.println("%%%%%%%%%%"); } }
注释掉这一行,不然让main线程sleep,前面设置的thread的priority,mian的yield()都不起作用???
首先yield 方法只是使当前线程重新回到可执行状态,所以Thread还是有可能在进入到可执行状态后马上又继续执行的。
其次 setPriority方法设置优先权,只有当竞争同一资源时,优先权大的一方会优先执行。然而这边线程执行时间太短,基本也不存在竞争资源的问题。优先权控制并不怎么可取。