不爱生姜不吃醋⭐️
如果本文有什么错误的话欢迎在评论区中指正
与其明天开始,不如现在行动!
文章目录
- 🌴前言
- 🌴一、卖电影票
- 1.题目
- 2.分析
- 3.代码
- 🌴二、送礼物
- 1. 题目
- 2. 分析
- 3.代码
- 🌴三.打印奇数
- 1. 题目
- 2. 分析
- 3.代码
- 🌴四.抢红包
- 1. 题目
- 2. 分析
- 3.代码
- 🌴五.抽奖
- 1. 题目
- 2. 分析
- 3.代码
- 🌴总结
🌴前言
为了能够对多线程方面有一个更加深入和全面的了解,做五道多线程相关的题目,其中1-3简单,4-5题需要在方法上有些改进。
🌴一、卖电影票
1.题目
一共1000张电影票,有两个窗口在卖,每次卖票的时间为3000毫秒,要求:用多线程模拟卖票的过程并打印剩余电影票的数量。
2.分析
题目较为简单,用Thread多线程完成。
3.代码
自己定义一个类
public class MyThread extends Thread{static int ticket = 1000;@Overridepublic void run() {while (true){synchronized (MyThread.class){if (ticket == 0){break;}try {sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}ticket--;System.out.println(getName()+"正在卖票,还剩余"+ticket+"张票!");}}}
}
启动线程
public class Demo {public static void main(String[] args) {MyThread mt1 = new MyThread();MyThread mt2 = new MyThread();mt1.setName("窗口一");mt2.setName("窗口二");mt1.start();mt2.start();}
}
🌴二、送礼物
1. 题目
有100份礼物,由两个人同时发,当剩下的礼品小于10份的时候就停止发放,利用多线程模拟该过程并将线程的名字个礼物的剩余数量打印出来。
2. 分析
思路同题目一
3.代码
自己定义一个类
public class MyThread extends Thread{static int gift = 10000;@Overridepublic void run() {while (true) {synchronized (MyThread.class) {if (gift < 10) {break;}gift--;System.out.println(getName()+"正在发礼物,还剩余"+gift+"件礼物!");}}}
}
启动线程
public class Demo {public static void main(String[] args) {MyThread t1 = new MyThread();MyThread t2 = new MyThread();t1.setName("同学甲");t2.setName("同学乙");t1.start();t2.start();}
}
🌴三.打印奇数
1. 题目
同时开启两个线程,共同获取1-100之间的所有数字,要求:将输出所有的奇数。
2. 分析
思路同题目一
3.代码
自己定义一个类
public class MyThread extends Thread {static int num = 1;@Overridepublic void run() {while (true) {synchronized (MyThread.class) {if (num == 100) {break;}if (num % 2 != 0) {System.out.println("当前是线程"+getName()+"正在输出,数字:"+num);}num++;}}}
}
启动线程
public class Demo {public static void main(String[] args) {MyThread t1 = new MyThread();MyThread t2 = new MyThread();t1.setName("线程1");t2.setName("线程2");t1.start();t2.start();}
}
🌴四.抢红包
1. 题目
假设有100块被分成了3个红包,有五个人去抢。打印结果:
XXX抢到了XX元
XXX抢到了X元
X没抢到
2. 分析
其中,红包是共享数据,五个人是五条线程。
3.代码
自己定义一个类
public class MyThread extends Thread {static int money = 100;static int count = 3;static final int MIN = 1;@Overridepublic void run() {synchronized (MyThread.class) {int prize = 0;if (count == 0) {System.out.println(getName() + "没抢到!");} else {if (count == 1) {prize = money;} else {Random r = new Random();int bounds = money - (count - 1) * MIN + 1;prize = r.nextInt(bounds);}money -= prize;count--;System.out.println(getName() + "抢到" + prize + "元!");}}}
}
启动线程来抢红包
public class Demo {public static void main(String[] args) {MyThread t1 = new MyThread();MyThread t2 = new MyThread();MyThread t3 = new MyThread();MyThread t4 = new MyThread();MyThread t5 = new MyThread();t1.setName("小明");t2.setName("小红");t3.setName("小李");t4.setName("小强");t5.setName("小风");t1.start();t2.start();t3.start();t4.start();t5.start();}
}
🌴五.抽奖
1. 题目
有两个抽奖箱存放了奖励金额且奖励金额不重复,分别为:2,5,10,20,50,80,100,200,500,700,800。
如果有人抽中奖,那么就把抽中的奖项显示出来,例如:
抽奖箱1产生了10元奖励
抽奖箱2产生了200元奖励
抽奖箱1产生了800元奖励
2. 分析
两个抽奖箱是两个线程,随机抽取打印
3.代码
定义一个类
public class MyThread extends Thread{ArrayList<Integer> list = new ArrayList<>();public MyThread(ArrayList<Integer> list) {this.list = list;}@Overridepublic void run() {while (true) {synchronized (MyThread.class) {if (list.size() == 0) {break;}Collections.shuffle(list);Integer prize = list.remove(0);System.out.println(getName()+"产生了"+prize+"元大奖!");try {sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}}}
}
启动线程
public class Demo {public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();Collections.addAll(list,2,5,10,20,50,80,100,200,500,700,800);MyThread t1 = new MyThread(list);MyThread t2 = new MyThread(list);t1.setName("奖箱1");t2.setName("奖箱2");t1.start();t2.start();}
}
🌴总结
文章中代码的编写使用的都是Java多线程和集合方面的知识,多加练习熟能生巧。
本文中若是有出现的错误请在评论区或者私信指出,我再进行改正优化,如果文章对你有所帮助,请给博主一个宝贵的三连,感谢大家😘!!!