这是一个在并发状态下对共享资源的多线程访问。这些访问必须是互斥的进行,因为涉及到对资源的修改。假设现在销售火车票。目的地有 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 tickets 表示目前可销售的车票种类,它通过一个 static 块初始化。 Map records 是当前售票厅的火车票销售情况。 List 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 tickets = new HashMap();
    private Map records ;
    private static List offices = new ArrayList();
    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();
    }
 
    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 " + 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();
    }
}
结果:
i want a ticket for BEIJING
i want a ticket for SHANGHAI
i want a ticket for TIANJING
i want a ticket for SHANGHAI
i want a ticket for SHANGHAI
sold a ticket for BEIJING
BEIJING tickets still have 4
i want a ticket for TIANJING
sold a ticket for SHANGHAI
SHANGHAI tickets still have 4
i want a ticket for SHANGHAI
sold a ticket for TIANJING
TIANJING tickets still have 4
i want a ticket for BEIJING
sold a ticket for SHANGHAI
SHANGHAI tickets still have 3
i want a ticket for TIANJING
sold a ticket for SHANGHAI
SHANGHAI tickets still have 2
i want a ticket for BEIJING
sold a ticket for BEIJING
BEIJING tickets still have 3
i want a ticket for SHANGHAI
sold a ticket for TIANJING
TIANJING tickets still have 3
i want a ticket for TIANJING
sold a ticket for BEIJING
BEIJING tickets still have 2
i want a ticket for BEIJING
sold a ticket for SHANGHAI
SHANGHAI tickets still have 1
i want a ticket for SHANGHAI
sold a ticket for TIANJING
TIANJING tickets still have 2
i want a ticket for SHANGHAI
sold a ticket for SHANGHAI
SHANGHAI tickets still have 0
i want a ticket for BEIJING
sold a ticket for TIANJING
TIANJING tickets still have 1
i want a ticket for BEIJING
sold a ticket for BEIJING
BEIJING tickets still have 1
i want a ticket for SHANGHAI
tickets for SHANGHAI have been sold out.
i want a ticket for SHANGHAI
tickets for SHANGHAI have been sold out.
i want a ticket for SHANGHAI
sold a ticket for BEIJING
BEIJING tickets still have 0
i want a ticket for BEIJING
tickets for BEIJING have been sold out.
i want a ticket for BEIJING
tickets for SHANGHAI have been sold out.
i want a ticket for SHANGHAI
tickets for SHANGHAI have been sold out.
i want a ticket for BEIJING
tickets for SHANGHAI have been sold out.
i want a ticket for SHANGHAI
tickets for BEIJING have been sold out.
closed
totally sold tickets:4,sell records:{ BEIJING =2, SHANGHAI =2}
tickets for BEIJING have been sold out.
closed
totally sold tickets:3,sell records:{TIANJING=2, SHANGHAI =1}
tickets for SHANGHAI have been sold out.
closed
totally sold tickets:3,sell records:{ BEIJING =2, TIANJING=1}
tickets for BEIJING have been sold out.
closed
totally sold tickets:2,sell records:{ SHANGHAI =2}
tickets for SHANGHAI have been sold out.
closed
totally sold tickets:2,sell records:{ BEIJING =1, TIANJING=1}