线程的两种创建方法以及Thread类的sleep(),yield(),getPriority(),setPriority(),start()方法MAX_PRIORITY,MIN_PRIORITY,NORM_PRIORITY常量的使用方法
class TestA extends Thread{
public void run(){
for(int i = 0;i < 100;i++){
System.out.println("TestA" + i);
}
}
}
class TestB implements Runnable{
public void run(){
for(int i = 0;i < 100;i++){
System.out.println("TestB" + i);
if(i == 80){
try{
Thread.sleep(2 * 1000);//b线程休眠2秒
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
}
class TestDemo{
public static void main(String args[]){
TestA a = new TestA();//第一种创建线程方法
Thread b = new Thread(new TestB());//第二种创建线程方法
System.out.println(a.NORM_PRIORITY);//默认优先级为5
System.out.println(b.getPriority());//取得线程当前优先级;
a.setPriority(10);//最高优先级为10
b.setPriority(1);//最低优先级为1
a.yield();//a线程让出cpu然后继续和b线程抢占cpu
a.start();//启动线程
b.start();
}
}