、线程的优先级
1.MAX_PRIORITY:10
MIN_PRIORITY:1
NORM_PRIORITY:5//默认优先级
2. 如何获得和设置当前线程的优先级:
//getPriority()//获取线程的优先级
//setPriority(int p)//设置线程的优先级
//说明:高优先级会抢占低优先级线程cpu的执行权,但是只是在概率上讲的,高优先级的线程高概率的情况下被执行,并不
//意味着只有当前只有高优先级执行完以后,低优先级才执行。
*******************************************************************************/
//例子:创建三个窗口卖票,总票数为100
class Window extends Thread{
private static int ticket =100;
public void run(){
while(true){
if(ticket>0){
System.out.println(getName()+"卖票,票号为:"+ticket);
ticket--;
}
else{
break;
}
}
}
}
public class Main
{
public static void main(String[] args) {
Window t1= new Window();
Window t2= new Window();
Window t3= new Window();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
//创建多线程的方式二:实现Runnable接口
//1.创建一个实现了Runnable接口的类
//2.实现类去实现Runnable中抽象的方法:run()
//3.创建类的对象
//4.将此对象作为参数传递到Thread类的构造器中,创建Thread类对象中。
//5.通过Thread类的对象调用start。
//1.创建一个实现了Runnable接口的类
class MThread implements Runnable{
//2.实现类去实现Runnable中抽象的方法:run()
public void run(){
ffor (int i=0;i<100 ;i++ ){
System.out.println(i);
}
}
}
public class Main{
public static void main(String[] args){
//3.创建类的对象
MThread mThead = new MThread();
//4.将此对象作为参数传递到Thread类的构造器中,创建Thread类对象中。
Thread t1 = new Thread(mThead);
//5.通过Thread类的对象调用start。
t1.start();
}
}