练习题10-
1、模拟上车案例:有一辆车,有100个座位,有前中后三个门每个门都可以随机上人,上人的个数不确定。
package com.xxx; import java.util.Random; public class CarRunnable implements Runnable { private static int seat = 100; //空座位数 private static int passenger = 0; //乘客数量 private static String lock = "锁"; @Override public void run() { while (true) { synchronized (lock) { if (seat > 0) { passenger++; seat--; System.out.println("第" + passenger + "位乘客从" + Thread.currentThread().getName() + "上车,座位剩余:" + seat); } else { break; } } try { //睡一下给其他线程留机会 //给从其他门进来的乘客抢座的机会 Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } // Thread.yield(); } } } package com.xxx; public class Homework1 { public static void main(String[] args) { CarRunnable door = new CarRunnable(); Thread t1 = new Thread(door,"①号门"); Thread t2 = new Thread(door,"②号门"); Thread t3 = new Thread(door,"③号门"); t1.start(); t2.start(); t3.start(); } }
2、实现两个线程打印交替打印1-100
package com.xxx; public class PrintRunnable implements Runnable { private int num = 1; @Override public void run() { while (true) { synchronized (this) { notify(); if (num <= 100) { System.out.println(Thread.currentThread().getName() + ":" + num); num++; try { // Thread.sleep(100); this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { break; } } } } } package com.xxx; public class Homework2 { public static void main(String[] args) throws InterruptedException { PrintRunnable print = new PrintRunnable(); new Thread(print,"1号打印机").start(); new Thread(print,"2号打印机").start(); } }
3、模拟多线程中的生产者与消费者模式
package com.xxx.homework3; public class BreadShop { private int bread = 10; /* 生产面包 */ public synchronized void makeBread() { if (bread <= 10) {//假设面包库存10个 bread++; System.out.println(Thread.currentThread().getName() + "制作一个面包》》,店里目前有 " + bread + " 个面包"); notify();//唤醒吃面包的线程,面包做好了 } else { try { wait();//面包库存够了,不用制作面包 } catch (InterruptedException e) { e.printStackTrace(); } } } /* 吃面包 */ public synchronized void eatBread() { if (bread > 0) { bread--; System.out.println(Thread.currentThread().getName() + "吃掉一个面包**,店里还剩 " + bread + " 个面包"); notify();//唤醒制作面包的线程 } else { try { wait();//等一会儿,面包还没做好 } catch (InterruptedException e) { e.printStackTrace(); } } } } package com.xxx.homework3; public class Customer implements Runnable { private BreadShop breadShop; public Customer(BreadShop breadShop) { this.breadShop = breadShop; } @Override public void run() { System.out.println(Thread.currentThread().getName() + "要吃面包"); while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this) { breadShop.eatBread(); } } } } package com.xxx.homework3; public class Producer implements Runnable { private BreadShop breadShop; public Producer(BreadShop breadShop) { this.breadShop = breadShop; } @Override public void run() { System.out.println(Thread.currentThread().getName() + "开始做面包》》》"); while (true) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } breadShop.makeBread(); } } } package com.xxx.homework3; public class Test { public static void main(String[] args) { BreadShop breadShop = new BreadShop(); Customer customer = new Customer(breadShop); Producer producer = new Producer(breadShop); new Thread(producer,"生产者").start(); new Thread(customer,"消费者1").start(); new Thread(customer,"消费者2").start(); new Thread(customer,"消费者3").start(); } }
4、使用多线程的的两种方式求出1-100偶数之和
package com.xxx.homework4; public class Summation implements Runnable{ private int sum = 0; private int num = 0; /* 求和线程 */ @Override public void run() { if (num <= 100) { synchronized (this) { for (int i = 0; i <= 100; i+=2) { if (i % 2 == 0) { sum += i; num++; } } } System.out.println(Thread.currentThread().getName() + "-> 100内偶数和:" + sum); } else { return; } } } package com.xxx.homework4; public class Test { public static void main(String[] args) { Summation summation = new Summation(); new Thread(summation,"偶数线程").start(); } } package com.xxx.homework4; public class Test2 { private static int oddSum = 0; private static int evenSum = 0; public static void main(String[] args) { // final int sum = 0; new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= 100; i+=2) { oddSum+=i; } System.out.println(Thread.currentThread().getName() + "->求和" + oddSum); } },"偶数线程").start(); new Thread(new Runnable() { @Override public void run() { for (int j = 1; j <= 100; j+=2) { evenSum+=j; } System.out.println(Thread.currentThread().getName() + "->求和" + evenSum); } },"奇数线程").start(); } }
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16819981.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步