Create a single "Train Ticket class"
Package date0804.demo3;/** * Ticket * @author ZZW922CN * */public Final class Ticket {private final String name;public Ticket (S Tring name) {super (); this.name = name;} Public String GetName () {return name;}}
Construction of train ticket DB
Mode of production of train tickets using static factory:
Package Date0804.demo3;import Java.io.bytearrayinputstream;import Java.io.inputstream;import Java.security.digestinputstream;import Java.security.messagedigest;import Java.util.arraylist;import Javax.xml.bind.datatypeconverter;public class Ticketfactory {private arraylist<ticket> list = new ArrayList< Ticket> ();p rivate String str= "seed";p rivate final int number_of_tickets=100;private ticketfactory () {//Generate train ticket for ( int count=0;count<number_of_tickets;count++) {InputStream InputStream = new Bytearrayinputstream (Str.getBytes ()); MessageDigest Instance;try {instance = messagedigest.getinstance ("MD2");D igestinputstream digestinputstream = new Dige Stinputstream (InputStream, instance); while (Digestinputstream.read ()!=-1);d igestinputstream.close (); byte[] Digest = Instance.digest (); Str=datatypeconverter.printhexbinary (Digest); List.add (new Ticket (str));} catch (Exception ex) {ex.printstacktrace ();}}} private static Ticketfactory ticketfactory=new ticketfactory ();p ublic static ARraylist<ticket> Gettickets () {return ticketfactory.list;}}
Operation of train tickets and changes in db
Package date0804.demo3;/** * Ticketing system */import java.util.arraylist;import java.util.hashmap;import java.util.Map;/** * Multi-threaded analogue train ticket ticketing system * @author ZZW922CN * */public class Ticketoperation {private arraylist<ticket> list;public Ticketopera tion (arraylist<ticket> list) {this.list=list;} Public map<integer,ticket> Sell () {synchronized (this) {Hashmap<integer, ticket> hashmap=new hashmap< Integer, ticket> (), if (List.size () >0) {Ticket ticket=list.get (0); list.remove (0); int size=list.size (); Hashmap.put (size, ticket); return hashMap;} else {return null;}}}}
Ticket Purchase behavior thread
Package Date0804.demo3;import java.io.file;/** * Buy tickets */import java.util.iterator;import Java.util.map;import Java.util.map.entry;import Java.util.set;public class Buyticketrunnable implements Runnable {private ticketoperation Ticketdb;private String content; File File=new file ("Ticket.log");p ublic buyticketrunnable (ticketoperation ticketdb) {this.ticketdb = Ticketdb;} @Overridepublic void Run () {map<integer,ticket> Map=ticketdb.sell (); if (map!=null) {Set<entry<integer, ticket>> EntrySet = Map.entryset (); Iterator<entry<integer, ticket>> Iterator2 = EntrySet.iterator () ; Entry<integer, ticket> next = Iterator2.next (); int Size=next.getkey (); Ticket Ticket=next.getvalue (); content= "Thread" +thread.currentthread (). GetId () + "bought a ticket:" +ticket.getname () + "number of votes left" +size;} else {content= "Thread" +thread.currentthread (). GetId () + "ticket sold Out";} Log Writingthread writingthread = new Writingthread (file,content); Writingthread.start ();}}
Log records
Package date0804.demo3;/** * Log */import java.io.bufferedwriter;import java.io.file;import java.io.FileWriter; Import Java.io.ioexception;import Java.io.writer;import Java.util.date;public class Loggingfile {private Writer out; Public Loggingfile (File f) throws IOException {FileWriter FileWriter = new FileWriter (f,true);//Use buffer wrapper writer, can increase efficiency this.out = new BufferedWriter (fileWriter);} public void Writelogs (String message) throws IOException {synchronized (out) {date d=new date (); StringBuilder StringBuilder = new StringBuilder (d.tostring ()); Stringbuilder.append (' \ t '); Stringbuilder.append ( message); Stringbuilder.append ("\ r \ n"); Out.write (stringbuilder.tostring ()); System.out.println (Stringbuilder.tostring ());}} public void Close () throws IOException {Out.flush (); Out.close ();}}
Write thread for Log
Package date0804.demo3;/** * Log write file */import Java.io.file;import Java.io.ioexception;public class Writingthread extends Thread {Private file logfile;private String content;public writingthread (File logfile,string content) {this.logfile= Logfile;this.content=content;} @Overridepublic void Run () {try {loggingfile loggingfile = new Loggingfile (logFile); loggingfile.writelogs (content); Loggingfile.close ();} catch (IOException e) {e.printstacktrace ();}}}
Test class
Package Date0804.demo3;import Java.io.ioexception;import Java.security.nosuchalgorithmexception;import Java.util.arraylist;import Java.util.concurrent.executionexception;public class Test {private final static int number_ Of_buyers=120;public static void Main (string[] args) throws Interruptedexception, IOException, NoSuchAlgorithmException, Executionexception {arraylist<ticket> list=ticketfactory.gettickets (); Ticketoperation ticketoperation = new ticketoperation (list); Buyticketrunnable buyticketthread = new buyticketrunnable (ticketoperation); for (int i=0;i<number_of_buyers;i++) { Thread thread= new Thread (buyticketthread); Thread.Start ();}}}
Simple ticketing system using Java multi-line branches