JAVA 线程 之dota英雄买药

来源:互联网 发布:纪录片用哪个软件好 编辑:程序博客网 时间:2024/05/01 01:58



本文纯属娱乐,请勿当真!

玩dota的java新手。

cpu=魔兽控制中心(比如控制什么时候刷新野怪,技能CD等)

hp=红

shop=商店

hero=英雄


package com.dota.test2;import java.util.UUID;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Cpu implements Runnable {private static final Log log = LogFactory.getLog(Cpu.class);private Shop shop;public Cpu(Shop shop) {this.shop = shop;}@Overridepublic void run() {try {//生产药膏while(true){log.info("开始生产药膏");Hp hp = create();log.info(hp.getUuid()+":药膏生产完成");log.info("开始把药膏放入商店");add(hp);log.info(hp.getUuid()+":药膏放入商店完成");}} catch (Exception e) {e.printStackTrace();}}/*** * 生产药膏 * @return */public Hp create(){try {Thread.sleep(200);return new Hp(UUID.randomUUID().toString());} catch (Exception e) {e.printStackTrace();}return null;}public void add(Hp hp){try {synchronized (shop) {while(this.shop.getHp().size() >= this.shop.getMaxSize()){log.info(":商店已满,"+hp.getUuid()+":药膏等待放入");shop.wait();}this.shop.getHp().add(hp);//log.info("把药膏:"+hp.getUuid()+"放入商店");this.shop.notifyAll();}} catch (Exception e) {e.printStackTrace();}}}

package com.dota.test2;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Hp {private static final Log log = LogFactory.getLog(Hp.class);private String uuid;public Hp(String uuid) {this.uuid = uuid;}public String getUuid() {return uuid;}public void setUuid(String uuid) {this.uuid = uuid;}}

package com.dota.test2;import java.util.HashSet;import java.util.Set;public class Shop {private Set<Hp> hp = new HashSet<Hp>();private int maxSize = 3;public Set<Hp> getHp() {return hp;}public void setHp(Set<Hp> hp) {this.hp = hp;}public Shop() {// TODO Auto-generated constructor stub}public int getMaxSize() {return maxSize;}public void setMaxSize(int maxSize) {this.maxSize = maxSize;}}

package com.dota.test2;import java.util.Iterator;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Hero implements Runnable {private static final Log log = LogFactory.getLog(Hero.class);private Shop shop;private String name;public Hero(Shop shop,String name) {// TODO Auto-generated constructor stubthis.shop = shop;this.name = name;}@Overridepublic void run() {try {while(true){buy();}} catch (Exception e) {e.printStackTrace();}}//购买药膏private void buy(){try {synchronized (shop) {while (this.shop.getMaxSize() <= 0) {log.info(name + ":没有药膏了,需要等待");this.shop.wait();}Thread.sleep(1000);Iterator<Hp> set = this.shop.getHp().iterator();if(set.hasNext()){Hp  hp = set.next();this.shop.getHp().remove(hp);log.info(name + "购买的药膏id:" + hp.getUuid());this.shop.notifyAll();}}} catch (Exception e) {e.printStackTrace();}}}


运行:
package com.dota.test2;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class Test2 {private static final Log log = LogFactory.getLog(Test2.class);public static void main(String[] args) {Shop shop = new Shop();new Thread(new Cpu(shop)).start();new Thread(new Hero(shop,"VS")).start();new Thread(new Hero(shop,"WD")).start();new Thread(new Hero(shop,"ES")).start();new Thread(new Hero(shop,"AM")).start();}}




本文纯属娱乐,请勿当真!

玩dota的java新手。

cpu=魔兽控制中心(比如控制什么时候刷新野怪,技能CD等)

hp=红

shop=商店

hero=英雄

原创粉丝点击