线程练习题

1.模拟医院叫号。一天有50个普通号,10个专家号。专家号的优先级高,专家号看诊时间是普通号的2倍,当普通号叫到第10号的时候,把所有的所有的专家号叫完再叫普通号。

public class Test6 {
    public static void main(String[] args) {
        Thread thread2=new Thread(()->{
            for (int i=1;i<=10;i++) {
                try {
                    System.out.println("第" + i + "个" + Thread.currentThread().getName());
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"专家号");
        //普通号的线程
        Thread thread1=new Thread(()->{
            for(int i=1;i<=50;i++) {
                try {
                    System.out.println("第" + i + "个" + Thread.currentThread().getName());
                    Thread.sleep(1);
                    if (i == 9) {
                        thread2.join();
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"普通号");
        thread1.setPriority(Thread.MIN_PRIORITY);
        thread2.setPriority(Thread.MAX_PRIORITY);
        thread1.start();thread2.start();
    }
}

2.模拟火车站卖票。假设目前还剩10张票,3个窗口同时在卖。试输出售票记录,如窗口A售出1张票,现剩余9张。

public class Test7 implements Runnable{
    private   int ticket=10;
    @Override
    public void run(){
        int i=0;
        while (true) {
            synchronized (this) {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (ticket > 0) {
                    System.out.print(Thread.currentThread().getName() + "出售" + (++i) + "张票");
                    System.out.println("现剩余" + (--ticket) + "张");
                }
                if (ticket==0){
                    break;
                }
            }
        }
    public static void main(String[] args) {
        Test7 test7=new Test7();
        Thread thread1=new Thread(test7,"窗口A");
        Thread thread2=new Thread(test7,"窗口B");
        Thread thread3=new Thread(test7,"窗口C");
        thread1.start();thread2.start();thread3.start();
    }
}

3、某账户中有20万元,先有急事需要全部取出。ATM机上,一次最多只能取1万,可以分多次取,也可以在多台ATM上同时登录来取。 试用程序模拟在3台ATM上的取款记录 如:在1号ATM上取款1万,卡里余额19万 在2号ATM上取款1万,卡里余额18万 。

public class Test7 implements Runnable{
    private   int ticket=20;
    @Override
    public void run(){
        int i=0;
        while (true) {
            synchronized (this) {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (ticket > 0) {
                    System.out.print(Thread.currentThread().getName() + "取款" + (++i) + "w");
                    System.out.println("卡里余额" + (--ticket) + "w");
                }
                if (ticket==0){
                    break;
                }
            }
        }
    public static void main(String[] args) {
        Test7 test7=new Test7();
        Thread thread1=new Thread(test7,"1号ATM机");
        Thread thread2=new Thread(test7,"2号ATM机");
        Thread thread3=new Thread(test7,"3号ATM机");
        thread1.start();thread2.start();thread3.start();
    }
}

4.写一个线程安全的饿汉式和懒汉式:
在这里插入图片描述
在这里插入图片描述
5、编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。

public class Test9 {
    static final Object lock=new Object();
    public static void main(String[] args) {
        Thread thread1=new Thread(()->{
            int i=1;
            synchronized (lock) {
                while (i <= 52) {
                    System.out.println(i);
                    if (i % 2 == 0) {
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    i++;
                }
            }
        });
        Thread thread2=new Thread(()->{
            char i='A';
            synchronized (lock) {
                while (i <='Z') {
                    System.out.println(i);
                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                }
            }
        });
        thread1.start();thread2.start();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值