Java多线程练习三
public class ex5 { public static void main(String [] args) { thread5 t1 = new thread5(); thread5_1 t2 = new thread5_1(); t1.setPriority(Thread.NORM_PRIORITY+3); t1.start(); t2.start(); for(int i=1;i<=100;i++) { System.out.println("3---"+i); } } } class thread5 extends Thread { public void run() { for(int i =1;i<=100;i++) System.out.println("1---"+i); } } class thread5_1 extends Thread { public void run() { for(int i = 1;i<=100;i++) System.out.println("2---"+i); } }
测试了下线程优先级,后面的是线程yield方法练习
public class ex4 { public static void main(String [] args) { thread4 t1 = new thread4("tt1"); thread4 t2 = new thread4("tt2"); t1.start(); t2.start(); } } class thread4 extends Thread { thread4(String a) { super(a); } public void run() { for(int i=1;i<20;i++) { System.out.println("nihao"+getName()); if(i%3==0) yield(); } } }