标签:test lazy 时间 min width com adt 级别 inf
线程的优先级:分为了10个等级,默认是5,最高是10,最低是1
public class ThreadTest09 { public static void main(String[] args) { //看看优先级别,优先级级别高只是抢占到cpu的时间片相对多一些 System.out.println("最高优先级:"+ Thread.MAX_PRIORITY); System.out.println("最低优先级:"+ Thread.MIN_PRIORITY); System.out.println("默认优先级:"+ Thread.NORM_PRIORITY); Thread t = new Thread(new MyRunnable9()); t.setName("t"); //给t线程设置优先级,设置成最高的,大概率是t先执行的多 t.setPriority(10); t.start(); System.out.println(Thread.currentThread().getName()+ "的优先级是:" + Thread.currentThread().getPriority()); for(int i =0 ;i<1000;i++){ System.out.println(Thread.currentThread().getName() +"---》" + i); } } } class MyRunnable9 implements Runnable{ public void run() { System.out.println(Thread.currentThread().getName()+ "的优先级是:" + Thread.currentThread().getPriority()); for(int i =0 ;i<1000;i++){ System.out.println(Thread.currentThread().getName() +"---》" + i); } } }
运行结果:
线程让位: Thread.yield(); //当前线程让给主线程
public class ThreadTest10 { public static void main(String[] args) { Thread t = new Thread(new MyRunnable10()); t.setName("t"); t.start(); for(int i = 1 ; i<=10000; i++){ System.out.println(Thread.currentThread().getName() +"---》" + i); } } } class MyRunnable10 implements Runnable{ public void run() { //每100次让一下 for(int i = 1 ; i<=10000; i++){ if(i%100 == 0){ Thread.yield(); //当前线程让给主线程 } System.out.println(Thread.currentThread().getName() +"---》" + i); } } }
结果:在99,199,299....都会让一下
线程合并:合并到当前线程,当前线程受阻,t线程执行直到结束。 栈并没有合并,只是调度,协调一下
public class ThreadTest11 { public static void main(String[] args) { System.out.println("main begin"); Thread t = new Thread(new MyRunnable11()); t.setName("t"); t.start(); try { t.join(); //t合并到当前线程,当前线程受阻,t线程执行直到结束。 栈并没有合并,只是调度,协调一下 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("main over"); } } class MyRunnable11 implements Runnable{ public void run() { for(int i =0 ;i<100;i++){ System.out.println(Thread.currentThread().getName() +"---》" + i); } } }
结果:main over在最后执行
标签:test lazy 时间 min width com adt 级别 inf
原文地址:https://www.cnblogs.com/peiminer/p/13617839.html