网站建设及运营岗位要求,seo服务 收费,网站建设工作分解,wordpress网址导航主题:tuiwp不爱生姜不吃醋⭐️ 如果本文有什么错误的话欢迎在评论区中指正 与其明天开始#xff0c;不如现在行动#xff01; 文章目录 #x1f334;前言#x1f334;一、卖电影票1.题目2.分析3.代码 #x1f334;二、送礼物1. 题目2. 分析3.代码 #x1f334;三.打印奇数1. 题目2.… 不爱生姜不吃醋⭐️ 如果本文有什么错误的话欢迎在评论区中指正 与其明天开始不如现在行动 文章目录 前言一、卖电影票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. 题目 有两个抽奖箱存放了奖励金额且奖励金额不重复分别为2510205080100200500700800。 如果有人抽中奖那么就把抽中的奖项显示出来例如 抽奖箱1产生了10元奖励 抽奖箱2产生了200元奖励 抽奖箱1产生了800元奖励 2. 分析
两个抽奖箱是两个线程随机抽取打印
3.代码 定义一个类 public class MyThread extends Thread{ArrayListInteger list new ArrayList();public MyThread(ArrayListInteger 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) {ArrayListInteger 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多线程和集合方面的知识多加练习熟能生巧。 本文中若是有出现的错误请在评论区或者私信指出我再进行改正优化如果文章对你有所帮助请给博主一个宝贵的三连感谢大家
文章转载自: http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.zlff.cn.gov.cn.zlff.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.cniedu.com.gov.cn.cniedu.com