线程创建之方法二

、线程的优先级
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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风strong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值