驾校学车-----多线程api基本使用

多线程驾校学车练习题

package com.cd.thread;

/**
 * @author Chen zhi xiang
 * @date 2020/5/26 19:17
 */

/**
 * 需求:模拟学院学车,10个Vip,10个普通学员。
 * Vip在普通学院之前结束学车,并且Vip学员的时间比普通的长
 */
class VipStudent implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            System.out.println(i+"vip正在学车。。。。。");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class NormalStudent implements Runnable{
    Thread vip ;
    public NormalStudent(Thread vip) {
        this.vip = vip;
    }
    public NormalStudent() {

    }
    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            //保证vip普通学员前学完
            if (i==9){
                try {
                    vip.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(i+"普通学员正在学车。。。。。");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class ThreadDemo03 {
    public static void main(String[] args) {
        VipStudent vipStudent = new VipStudent();
        Thread Vipthread = new Thread(vipStudent);

        NormalStudent normalStudent = new NormalStudent(Vipthread);
        Thread Normalthread = new Thread(normalStudent);
        //设置优先级
        Vipthread.setPriority(Thread.MAX_PRIORITY);
        Normalthread.setPriority(Thread.MIN_PRIORITY);

        Vipthread.start();
        Normalthread.start();

    }
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值