import java.util.HashMap;
//这里是商品仓库
public class GoodsStash implements Runnable{
HashMap<Integer,String> sp=new HashMap<>();
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("售卖" + sp.get(i));
}else {
System.out.println("目前没有商品");
}
}
}
public void warehousing(){
for (int i=0;i<100;i++){
sp.put(i,"商品--->"+i);
System.out.println("商品--->"+i+"入库");
}
}
}
添加GoodsStash作为可以操作的资源
用一个HashMap数组储存商品资源
warehousing()方法添加商品资源
run()方法消耗商品
public class Activity618 {
public void Activity618(){
GoodsStash gs=new GoodsStash();
Thread t=new Thread(gs);
t.start();
gs.warehousing();
}
}
在Activity618中开一个分支栈进行商品售卖主栈进行入库添加商品
import java.util.HashMap;
public class Activity618 {
public void Activity618(){
HashMap<Integer,String> sp=new HashMap<>();
// GoodsStash gs=new GoodsStash();
// Thread t=new Thread(gs);
//
// t.start();
// gs.warehousing();
Thread t=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("商家"+"售卖" + sp.get(i));
}else {
System.out.println("目前没有商品");
}
}
}
};
t.start();
for (int i=0;i<100;i++){
sp.put(i,"商品--->"+i);
System.out.println("商品--->"+i+"入库");
}
}
}
当然我们可以利用匿名内部类都写在Activity618中
import java.util.HashMap;
public class Activity618 {
public void Activity618(){
HashMap<Integer,String> sp=new HashMap<>();
// GoodsStash gs=new GoodsStash();
// Thread t=new Thread(gs);
//
// t.start();
// gs.warehousing();
Thread t=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("商家1号正在售卖" + sp.get(i));
}else {
System.out.println("目前没有商品");
}
}
}
};
Thread t1=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("商家2号正在售卖" + sp.get(i));
}else {
System.out.println("目前没有商品");
}
}
}
};
t.start();
t1.start();
for (int i=0;i<100;i++){
sp.put(i,"商品--->"+i);
System.out.println("商品--->"+i+"入库");
}
}
}
然后我们可以开辟两个分支栈共同售卖商品
import java.util.HashMap;
public class Activity618 {
public void Activity618(){
HashMap<Integer,String> sp=new HashMap<>();//总仓库
HashMap<Integer,String> sp1=new HashMap<>();//用户仓库
boolean goodsifsale=false;
// GoodsStash gs=new GoodsStash();
// Thread t=new Thread(gs);
//
// t.start();
// gs.warehousing();
Thread t=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("商家1号正在售卖" + sp.get(i));
sp.remove(i);sp1.put(i,"商品--->"+i);
}else {
System.out.println("目前没有商品");
}
}
}
};
Thread t1=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp.get(i)!=null) {
System.out.println("商家2号正在售卖" + sp.get(i));
sp.remove(i);sp1.put(i,"商品--->"+i);
}else {
System.out.println("目前没有商品");
}
}
}
};
t.start();
t1.start();
for (int i=0;i<100;i++){
sp.put(i,"商品--->"+i);
System.out.println("商品--->"+i+"入库");
}
Thread u=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp1.get(i)!=null) {
System.out.println("用户1号正在抢购" + sp1.get(i));
sp1.remove(i);
}else {
System.out.println("商品已出售");
}
}
}
};
Thread u1=new Thread(){
@Override
public void run() {
for (int i=0;i<100;i++){
if (sp1.get(i)!=null) {
System.out.println("用户2号正在抢购" + sp1.get(i));
sp1.remove(i);
}else {
System.out.println("商品已出售");
}
}
}
};
for (int i=10;i>=0;i--){
System.out.println("还有"+i+"秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
u.start();
u1.start();
}
}
添加两个商户和两个用户
用户等待10秒购买商品
商户在此之前准备商品