這是一個在並發狀態下對共享資源的多線程訪問。這些訪問必須是互斥的進行,因為涉及到對資源的修改。假設現在銷售火車票。目的地有3個,分別是北京,天津和上海。使用一個枚舉類型表示。
Destation.java
package com.zj.tickets;
public enum Destation {
BEIJING, SHANGHAI, TIANJING
}
下面設計車票類,final int original表示車票基數;int current表示當前還有多少張票;Destation destation表示目的地,它必須從上面的枚舉型中取出其一。
Ticket.java
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;
}
}
下面設計售票廳類,Map<Destation, Ticket> tickets表示目前可銷售的車票種類,它通過一個static塊初始化。Map<Destation, Integer> records是當前售票廳的火車票銷售情況。List<BookingOffice> offices表示當前可工作的所有售票廳,每個售票廳是一個線程。int ticketsSold表示當前售票廳售出的火車票總數。int id表示當前售票廳編號。
每個線程都會模擬客戶買票、工作人員查詢數據庫和售票交易過程。
模擬的數據為,當前發往北京、上海和天津的火車票各5張;每個售票廳交易5次(無論交易成功或失敗)後關閉。
BookingOffice.java
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);
}
}
模擬售票過程如下,啟動5個售票廳。
Sell.java
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();
}
}
結果:
<Officce-0>i want a ticket for BEIJING
<Officce-1>i want a ticket for SHANGHAI
<Officce-2>i want a ticket for TIANJING
<Officce-3>i want a ticket for SHANGHAI
<Officce-4>i want a ticket for SHANGHAI
<Officce-0>sold a ticket for BEIJING
<Officce-0>BEIJING tickets still have 4
<Officce-0>i want a ticket for TIANJING
<Officce-1>sold a ticket for SHANGHAI
<Officce-1>SHANGHAI tickets still have 4
<Officce-1>i want a ticket for SHANGHAI
<Officce-2>sold a ticket for TIANJING
<Officce-2>TIANJING tickets still have 4
<Officce-2>i want a ticket for BEIJING
<Officce-3>sold a ticket for SHANGHAI
<Officce-3>SHANGHAI tickets still have 3
<Officce-3>i want a ticket for TIANJING
<Officce-4>sold a ticket for SHANGHAI
<Officce-4>SHANGHAI tickets still have 2
<Officce-4>i want a ticket for BEIJING
<Officce-4>sold a ticket for BEIJING
<Officce-4>BEIJING tickets still have 3
<Officce-4>i want a ticket for SHANGHAI
<Officce-3>sold a ticket for TIANJING
<Officce-3>TIANJING tickets still have 3
<Officce-3>i want a ticket for TIANJING
<Officce-2>sold a ticket for BEIJING
<Officce-2>BEIJING tickets still have 2
<Officce-2>i want a ticket for BEIJING
<Officce-1>sold a ticket for SHANGHAI
<Officce-1>SHANGHAI tickets still have 1
<Officce-1>i want a ticket for SHANGHAI
<Officce-0>sold a ticket for TIANJING
<Officce-0>TIANJING tickets still have 2
<Officce-0>i want a ticket for SHANGHAI
<Officce-4>sold a ticket for SHANGHAI
<Officce-4>SHANGHAI tickets still have 0
<Officce-4>i want a ticket for BEIJING
<Officce-3>sold a ticket for TIANJING
<Officce-3>TIANJING tickets still have 1
<Officce-3>i want a ticket for BEIJING
<Officce-2>sold a ticket for BEIJING
<Officce-2>BEIJING tickets still have 1
<Officce-2>i want a ticket for SHANGHAI
<Officce-1>tickets for SHANGHAI have been sold out.
<Officce-1>i want a ticket for SHANGHAI
<Officce-0>tickets for SHANGHAI have been sold out.
<Officce-0>i want a ticket for SHANGHAI
<Officce-4>sold a ticket for BEIJING
<Officce-4>BEIJING tickets still have 0
<Officce-4>i want a ticket for BEIJING
<Officce-3>tickets for BEIJING have been sold out.
<Officce-3>i want a ticket for BEIJING
<Officce-2>tickets for SHANGHAI have been sold out.
<Officce-2>i want a ticket for SHANGHAI
<Officce-1>tickets for SHANGHAI have been sold out.
<Officce-1>i want a ticket for BEIJING
<Officce-0>tickets for SHANGHAI have been sold out.
<Officce-0>i want a ticket for SHANGHAI
<Officce-4>tickets for BEIJING have been sold out.
<Officce-4>closed
<Officce-4>totally sold tickets:4,sell records:{BEIJING=2, SHANGHAI=2}
<Officce-3>tickets for BEIJING have been sold out.
<Officce-3>closed
<Officce-3>totally sold tickets:3,sell records:{TIANJING=2, SHANGHAI=1}
<Officce-2>tickets for SHANGHAI have been sold out.
<Officce-2>closed
<Officce-2>totally sold tickets:3,sell records:{BEIJING=2, TIANJING=1}
<Officce-1>tickets for BEIJING have been sold out.
<Officce-1>closed
<Officce-1>totally sold tickets:2,sell records:{SHANGHAI=2}
<Officce-0>tickets for SHANGHAI have been sold out.
<Officce-0>closed
<Officce-0>totally sold tickets:2,sell records:{BEIJING=1, TIANJING=1}
本文出自 “子 孑” 博客,請務必保留此出處http://zhangjunhd.blog.51cto.com/113473/70767