模拟高校的三个老师同时分发80份学习笔记

来源:互联网 发布:怎么看自己的淘宝信誉 编辑:程序博客网 时间:2024/05/17 23:06
10、模拟高校的三个老师同时分发80份学习笔记,每个老师相当于一个线程。
public class Test01 {public static void main(String[] args) {Teacher t = new Teacher();new Thread(t, "陈老师").start();new Thread(t, "高老师").start();new Thread(t, "李老师").start();}}class Teacher implements Runnable {private int notes = 80;public void run() {while (true) {dispatchNotes(); // 调用售票方法if (notes <= 0) {break;}}}private synchronized void dispatchNotes() {if (notes > 0) {try {Thread.sleep(10); // 经过的线程休眠10毫秒} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "---发出的笔记" + notes--);}}}


原创粉丝点击