/* 问题:模拟三个老师同时发放80份学习笔记,每个老师相当于一个线程. 知识:需要用到sychrnized() 同步方法 思路: 1.定义一个用sychrnized() 修饰的同步方法,该方法进行售票 2.创建一个线程实例,分别开启三个老师的线程 */ class Notes implements Runnable{ private int notes = 800; public void run(){ while(true){ distribute(); if(notes <= 0){ break; } } } //定义一个同步方法 private synchronized void distribute(){ if(notes > 0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"---发出笔记本: "+notes--); } } } public class Test{ public static void main(String []a){ Notes note = new Notes(); new Thread(note,"甲老师").start(); new Thread(note,"乙老师").start(); new Thread(note,"丙老师").start(); } }