1. A multi-threaded example:
Description: a train ticketing system, there are multiple windows, but only vote there in the same system:
Design ideas: there is only one system:
Details Design: single design pattern to ensure that a common multi-window object
Use Vector collection framework to ensure the safety of concurrent threads;
A plurality of ticket windows, use multiple threads simulate multiple windows, the method calls the run sellTicket () method of class System employed in combination.
code show as below:
// Ticket entity class public class Ticket { // start station, the end price // only some attributes of an object contains many properties, enhance readability // the JavaBean, POJOs String Start; String End; the Float. Price; // caps, packaging public Ticket (String Start, End String, the Float. price) { the this .start = Start; the this .end = End; the this .price = . price; } public Ticket () { } public String getStart () { return Start ; } public String getEnd() { return end; } public Float getPrice() { return price; } public void setStart(String start) { this.start = start; } public void setEnd(String end) { this.end = end; } public void setPrice(Float price) { this.price = price; } //重写toString 方法,为了打印对象方便 public String toString(){ StringBuilder sb=new new the StringBuilder (); sb.append ( the this . .start) .append ( "->") the append ( the this .end) .append ( "Price") .append ( the this .price); return new new String (SB); } } // SystemTest class public class SystemTest { // only one system: design singleton Private SystemTest () { } Private static SystemTest ST = new new SystemTest (); // static: guarantee uniqueness public static SystemTest the getInstance () { // static methods: you can call by class name returnST; } // attribute, set the ArrayList, the Vector -> the synchronized, Stack Private the Vector <Ticket> = tickets new new the Vector <> (); // the current system to create, assign a set of tickets, with complete block { for ( int 10 = I; I <100; I ++ ) { tickets.add ( new new Ticket ( "Beijing" + i, "Shenzhen" I +, I +. 5%. 5 + 25F is)); } } // design a method, from a set of take the ticket public ticket getTicket () { the try { return tickets.remove (0 ); } the catch (Exception E) { return null ; // no vote situation } } } // Window Windows-based public class the Window the extends the Thread { String windowName; public the Window (String windowName) { the this .windowName = windowName; } @Override public void RUN () { // Sell votes sellTicket (); } public void sellTicket () { the while ( to true ) { SystemTest ST =SystemTest.getInstance (); // get singletons Ticket T = st.getTicket (); // obtaining tickets from the collection Vector IF (T == null ) { System.out.println (windowName + "window ticket has been sold End " ); BREAK ; } System.out.println (windowName +" sold: "+ T); } } } // the testMain test main class public class the testMain { public static void main (String [] args) { // there are a number of windows, each of a total system W1 = Window new new Window ( "Beijing Railway Station" ); Window W2 = new new Window ( "Xi'an station" ); Window W3 = new new Window ( "Chongqing Station" ); w1.start (); w2.start (); W3. Start (); } }
2. turns print ABCABCABC ....... 10 times:
Locked to the subject using synchronized manner, so that each thread object access methods can not be performed simultaneously, one by one to obtain.
具体:synchronized(Object){ }
// class Test class that is is to perform printing // turns lock // thread class is a thread running up public class Test the implements Runnable { Private String name; // EG: "A" Private Object pre; // OA, ob, oc circulated between before and after the three objects Private Object Self; // own public the Test (String name, pre Object, Object Self) { the this .name = name; the this .PRE = pre; the this .self = Self; } / / each thread has run method of each thread. @Override public void RUN () { int count = 10; // count down 10 times to print the while (count> 0 ) { the synchronized (pre) { the synchronized (Self) { of System.out.print (name); // block lock count - ; self.notify (); } the try { pre.wait (); } the catch (InterruptedException E) { e.printStackTrace (); } } } } // @Override // public void RUN () { // the synchronized (pre) { // the synchronized (Self) { // here is locked while the loop // // first write logic itself locked in consideration problems // int COUNT = 10; // the while (COUNT> 0) { // System.out.println (name + COUNT); // count--; // self.notify (); // } // } // the try { // pre.wait (); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } // } } //测试主类: public class Main { public static void main(String[] args) throws InterruptedException { Object oa=new Object(); Object ob=new Object(); Object oc=new Object(); new Thread(new Test("A",oc,oa)).start(); Thread.sleep(1000); new Thread(new Test("B",oa,ob)).start(); Thread.sleep(1000); new Thread(new Test("C",ob,oc)).start(); Thread.sleep(1000); } }
3. Analog philosopher eating problems:
page