thread wait notify
来源:互联网 发布:数据库表主键的作用 编辑:程序博客网 时间:2024/05/02 00:13
Athlete.java
package thread_multy.demo_wait_notify_01;class Athlete implements Runnable{private final int id;private Game game;/** * 选手 * * @param id 编号 * @param game 比赛 */public Athlete(int id, Game game){this.id = id;this.game = game;}public boolean equals(Object o){if (!(o instanceof Athlete)){return false;}Athlete athlete = (Athlete) o;return id == athlete.id;}public String toString(){return "Athlete<" + id + ">";}public int hashCode(){return new Integer(id).hashCode();}public void run(){try{game.prepare(this);}catch (InterruptedException e){System.out.println(this + " quit the game");}}}
Game.java
package thread_multy.demo_wait_notify_01;import java.util.Collection;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/** * 模拟线程之间的协作。Game类有2个同步方法prepare()和go()。 * 标志位start用于判断当前线程是否需要wait()。 * Game类的实例首先启动所有的Athele类实例,使其进入wait()状态, * 在一段时间后,改变标志位并notifyAll()所有处于wait状态的Athele线程。 * * * @author Administrator * */public class Game implements Runnable{private Set<Athlete> players = new HashSet<Athlete>();private boolean start = false;/** * * @param one */public void addPlayer(Athlete one){players.add(one);}public void removePlayer(Athlete one){players.remove(one);}/** * 2 */public void run(){start = false;System.out.println("Ready......");System.out.println("Ready......");System.out.println("Ready......");ready();//预备start = true;System.out.println("Go!");go();//跑}/** * 3 */public void ready(){Iterator<Athlete> iter = getPlayers().iterator();while (iter.hasNext()){new Thread(iter.next()).start();}}/** * 4 * * @return */public Collection<Athlete> getPlayers(){return Collections.unmodifiableSet(players);}/** * synchronized method * * @param athlete * @throws InterruptedException */public void prepare(Athlete athlete) throws InterruptedException{System.out.println(athlete + " ready!");synchronized (this){while (!start){wait();}if (start){System.out.println(athlete + " go!");}}}/** * synchronized method */public synchronized void go(){notifyAll();}/** * 测试 * * @param args */public static void main(String[] args){Game game = new Game();// 比赛参加选手 0 1 2 3 4 5 6 7 8 9 10个人for (int i = 0; i < 10; i++){//game.addPlayer(new Athlete(i, game));}new Thread(game).start();}}
阅读全文
0 0
- Thread, wait(), notify(), sleep()
- Thread wait notify
- Thread wait notify用法
- thread wait notify
- Thread线程间通讯-wait,notify
- Java Thread wait, notify and notifyAll Example
- Java Thread wait, notify和notifyAll示例
- Thread详解8:wait/notify机制
- thread的wait\notify实现线程通信
- thread的wait\notify实现线程通信
- java thread中的wait()和notify()
- CountDownLatch,interrupt, wait, notify, Thread各方法
- Thread:线程之间的通信,使用wait()和notify()
- Thread的wait sleep join yield suspend notify区别
- Thread的wait sleep join yield suspend notify区别
- Thread的wait sleep join yield suspend notify区别收藏
- Java 线程 Thread 对象锁的wait和notify解析
- [转]一个关于Java Thread wait(),notify()的实用例
- 方言 —— 各地方言
- 提携别人,也是完善自己
- 自制操作系统
- 实习日报
- 利用tomcat创建虚拟的图片服务器
- thread wait notify
- 设计模式之代理模式
- 寻找第n个默尼森数
- windows用Chrome浏览器访问Centos7虚拟机中django项目执行结果
- FTPrep, 4 Median of Two Sorted Arrays
- Bootstrap学习心得
- ARC 059F バイナリハック DP
- [LeetCode] 328. Odd Even Linked List
- SMPCUP2017_linux_cmd