线程常用的方法
public class MyRunner3 extends Thread {@Overridepublic void run() {for(int i = 0; i < 5; i++){System.out.println("i am " + getName());try {sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}public class TestThread3 {public static void main(String[] args) {Thread thread = new MyRunner3();thread.start();try {//主线程等待thread的业务处理完了之后再向下运行//thread和主线程合并了thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}for(int i = 0; i < 100; i++){System.out.println("main : " + i);}}}?
public class MyRunner4 extends Thread {@Overridepublic void run() {for(int i = 0; i < 100; i++){System.out.println(getName() + " : " + i);if(i%10 == 0){System.out.println("ok");yield();}}}}public class TestThread4 {public static void main(String[] args) {Thread t1 = new MyRunner4();Thread t2 = new MyRunner4();t1.start();t2.start();}}?
线程的优先级用数字表示,范围从1到10,默认的是为5
优先级设置高了之后,获取CPU运行时间片要多一些,具体执行多少时间由操作系统决定
?
public class MyRunner5 extends Thread {@Overridepublic void run() {for(int i = 0; i < 1000; i++){System.out.println(getName() + " : " + i);}}}public class TestThread5 {public static void main(String[] args) {Thread t1 = new MyRunner5();Thread t2 = new MyRunner5();t2.setPriority(Thread.NORM_PRIORITY+3);t1.start();t2.start();}}?