Java:多线程模拟多站点售票过程
|
package com.zj.tickets;
public enum Destation {
}
|
|
package com.zj.tickets;
public class Ticket {
private final int original;
private int current;
private final Destation destation;
public Ticket(int nums, Destation
where) {
current = original = nums;
destation = where;
}
public int degress() {
return --current;
}
public int original() {
return original;
}
public boolean soldout() {
return current <= 0;
}
public Destation getDestation() {
return destation;
}
public int getCurrent() {
return current;
}
}
|
|
package com.zj.tickets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import
java.util.concurrent.TimeUnit;
public class BookingOffice implements Runnable {
private static Map<Destation, Ticket>
tickets = new HashMap<Destation, Ticket>();
private Map<Destation,
Integer> records;
private static List<BookingOffice> offices = new
ArrayList<BookingOffice>();
private int ticketsSold = 0;
private final int id;
// now
today's tickets for sell:
static {
tickets.put(Destation.BEIJING, new Ticket(5,
Destation.BEIJING));
tickets.put(Destation.SHANGHAI, new Ticket(5,
Destation.SHANGHAI));
tickets.put(Destation.TIANJING, new Ticket(5,
Destation.TIANJING));
}
public BookingOffice(int id) {
this.id = id;
offices.add(this);
resetRecords();
}
private void resetRecords() {
records = new HashMap<Destation, Integer>();
}
private void addRecords(Destation d) {
Integer freq = records.get(d);
records.put(d, freq == null ? 1 : freq + 1);
}
public void run() {
int transaction = 5;
while (transaction-- > 0) {
// simulate a customer's coming:
Destation d = Destation.values()[new
Random().nextInt(Destation
.values().length)];
print(this + "i want a ticket for " + d);
// simulate the officer's checking:
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// simulate the transaction:
Ticket wanted = tickets.get(d);
synchronized (wanted) {
if (!wanted.soldout()) {
print(this + "sold a ticket for " + d);
wanted.degress();
addRecords(d);
++ticketsSold;
print(this + "" + d + " tickets still have "
+ wanted.getCurrent());
} else
print(this + "tickets for " + d + " have been sold out.");
}
}
print(this + "closed");
print(this + "totally sold tickets:" + ticketsSold + ",sell
records:"
+ records);
}
public synchronized int getValue() {
return ticketsSold;
}
public String toString() {
return "<Officce-" + id + ">";
}
static void print(String s) {
System.out.println(s);
}
}
|
|
package com.zj.tickets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Sell {
public static void main(String[] args) throws Exception
{
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++)
exec.execute(new
BookingOffice(i));
exec.shutdown();
}
}
|
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70767
BookingOffice.run() 中的synchronized(this),
this 作为同步对象这里是没有任何意义的,因为它自己是个线程。
我觉得应该是synchronized(wanted),请确认一下。