三湘古邑

我想在那里最蓝的大海扬帆。

0%

多线程练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
Map<String, Integer> totalStock = new HashMap<>();
AtomicInteger total = new AtomicInteger();
private List<Car> carList = new ArrayList<>();

public void testSale() {
produceCar();
for (; ; ) {
if (total.get()>0) {
sleep(TimeUnit.MILLISECONDS,200);
saleCar();
} else {
System.out.println("所有库存都卖完了,快点进货吧!");
break;
}
}
}

private void saleCar() {
new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小红").start();

new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小张").start();

new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小明").start();

new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小花").start();

}

private void produceCar() {
System.out.println("开始进购汽车....");
carList.add(new Car("高尔夫", 10, 129888, 0.94f));
totalStock.put("高尔夫", 10);
total.addAndGet(10);
System.out.println("进购高尔夫 10");
carList.add(new Car("Polo", 10, 90998, 0.9f));
totalStock.put("Polo", 10);
total.addAndGet(10);
System.out.println("进购Polo 10");
carList.add(new Car("迈腾", 40, 186898, 0.9f));
totalStock.put("迈腾", 40);
total.addAndGet(40);
System.out.println("进购迈腾 40");
carList.add(new Car("途观", 30, 199088, 0.91f));
totalStock.put("途观", 30);
total.addAndGet(30);
System.out.println("进购途观 30");
System.out.println("共进购" + totalStock + "汽车,总库存" + total);
}

class Custom {

String name;
double capitalFund;

final List<String> cars = new ArrayList<>();

public Custom(String name, double capitalFund) {
this.name = name;
this.capitalFund = capitalFund;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getCapitalFund() {
return capitalFund;
}

public void setCapitalFund(double capitalFund) {
this.capitalFund = capitalFund;
}

public int buyCar(String carName, double carPrice) {

try {
double fund = capitalFund - carPrice;
if (fund >= 0d) {
cars.add("品牌:" + carName + ",价格:" + carPrice);
System.out.println(name + "买人了一辆" + carName + "剩余资产:" + NumberUtil.roundStr(fund, 2));
this.setCapitalFund(fund);
TimeUnit.MILLISECONDS.sleep(20);
return 1;
} else {
System.out.println(name + "想买人了一辆" + carName + "," +
"可是资产不足,总资产:" + NumberUtil.roundStr(capitalFund, 2) + ",需要资产:" + NumberUtil.roundStr(carPrice, 2));
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}


class Car {
String brandName;
int num;
int price;
double discount;

public Car(String brandName, int num, int price, double discount) {
this.brandName = brandName;
this.num = num;
this.price = price;
this.discount = discount;
}

public String getBrandName() {
return brandName;
}

public void setBrandName(String brandName) {
this.brandName = brandName;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public double getDiscount() {
return discount;
}

public void setDiscount(double discount) {
this.discount = discount;
}

Lock lock = new ReentrantLock();

public void sale(Custom custom) {
lock.lock();
try {
if (num > 0) {
double price = (this.price * this.discount) + (this.price / 1.17 * 0.1);
int r = custom.buyCar(this.getBrandName(), price);
if (r > 0) {
num--;
total.decrementAndGet();
totalStock.put(this.brandName, num);
System.out.println(Thread.currentThread().getName() + "卖给了" + custom.getName() + "一辆" + this.brandName + "车," + this.brandName + "剩余:" + num);
System.out.println("本店库存:" + totalStock + ",total:" + total);
TimeUnit.MILLISECONDS.sleep(10);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}

}
}

private void sleep(TimeUnit timeUnit, int time) {
try {
timeUnit.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}