首页 > 代码库 > java复习-线程让步

java复习-线程让步

  技术分享

改变线程的优先级

 

package Mypackage;

import java.util.Date;

import Myproject.Mysingleton;

public class Mythread extends Thread{
public Mythread(String name){
super(name);
}
public void run(){
for(int i=0;i<10;i++){
System.out.println("当前线程的名称:"+getName()+" "+i);
}
}
public static void main(String[] args) throws Exception{

Mythread mythread2=new Mythread("后台进程");
mythread2.setDaemon(true);
mythread2.setPriority(MAX_PRIORITY);//最大优先级 其值是10
mythread2.setPriority(MIN_PRIORITY);//最小优先级其值是1
mythread2.setPriority(NORM_PRIORITY);//其值是5
mythread2.start();
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
for(int i=0;i<10;i++){
System.out.println("当前时间:"+new Date());
Thread.sleep(1000);

}
}
}

 

java复习-线程让步