Yesterday the multi-threaded re-read again, found themselves still have a lot to understand the place, now write a summary.
One: The problem with inheriting the thread class is:
public class Sellticketsthread extends Thread {private int tickets = 10;public sellticketsthread (String name) {Super ( name);} @Overridepublic void Run () {while (true) {if (tickets<=0) break;synchronized (this) {try {thread.sleep ()}, catch (in Terruptedexception e) {e.printstacktrace ();} if (Tickets > 0) {System.out.println (Thread.CurrentThread (). GetName () + "sell First" + tickets--+ "Ticket");}}}} public class Sellticketsthreaddemo {public static void main (string[] args) {Sellticketsthread T1 = new Sellticketsthread (" Window 1 "); Sellticketsthread t2 = new Sellticketsthread ("Window 2"); sellticketsthread t3 = new Sellticketsthread ("Window 3"); T1.start (); T2.start (); T3.start ();}}
In this case, it appears that the object is locked with a synchronous lock, but the result is not logical. A total of 30 tickets were sold in three windows.
Window 2 sell 10th ticket window 1 sell 10th ticket window 3 sell 10th ticket window 3 sell 9th ticket window 2 sell 9th ticket window 1 sell 9th ticket window 3 sell 8th ticket window 1 sell 8th ticket window 2 sell 8th ticket window 2 sell 7th ticket window 3 sell 7th ticket window 1 sell 7th ticket window 2 sell 6th ticket windows Port 1 Sell 6th ticket window 3 sell 6th ticket window 2 sell 5th ticket window 1 sell 5th ticket window 3 sell 5th ticket window 2 sell 4th ticket window 3 sell 4th sheet Ticket window 1 sell 4th ticket window 2 sell 3rd ticket window 1 sell 3rd ticket window 3 sell 3rd ticket window 2 sell 2nd ticket window 1 sell 2nd ticket window 3 sell 2nd ticket window 2 sell 1th ticket window 3 sell 1th ticket window 1 sell 1th ticket
Second: Using the Runnale interface to achieve
Package Selltickets;public class Sellticketsthread implements runnable{private int tickets = ten; @Overridepublic void Run ( {while (true) {if (tickets<=0) break, synchronized (this) {try {Thread.Sleep],} catch (Interruptedexception e) { E.printstacktrace ();} if (tickets>0) {System.out.println (Thread.CurrentThread (). GetName () + "sell first" +tickets--+ "Ticket");}}}} public class Sellticketsthreaddemo {public static void main (string[] args) {Sellticketsthread st = new Sellticketsthread () ; thread T1 = new Thread (ST, "window One"); Thread t2 = new Thread (ST, "window Two"); thread t3 = new Thread (ST, "window Three"); T1.start (); T2.start (); T3.start ();}}
Window one sell 10th ticket window one sell 9th ticket window one sell 8th ticket window three sell 7th ticket window two sell 6th ticket window two sell 5th ticket window three sell 4th ticket window three sell 3rd ticket window three sell 2nd ticket
This will not be a problem, because three threads share the same runnable open memory. Online Almost all use this method, can not runnable interface can also achieve this effect?? Next is the use of thread implementation of two ways, if there are errors, please correct me.
Three: Define static variables and static methods in the class that inherits the thread, add locks to static methods, and allow multiple threads to share.
public class Sellticketsthread extends Thread {public sellticketsthread (String name) {super (name);} Public Sellticketsthread () {}private static int tickets =10;public synchronized static void Take () {try {thread.sleep (100) ;} catch (Interruptedexception e) {e.printstacktrace ();} if (tickets>0) {System.out.println (Thread.CurrentThread (). GetName () + "sell First" + tickets--+ "Ticket");}} @Overridepublic void Run () {while (true) {Take ()}}} public class Sellticketsthreaddemo {public static void main (string[] args) {Sellticketsthread T1 = new Sellticketsthread (" Window 1 "); Sellticketsthread t2 = new Sellticketsthread ("Window 2"); sellticketsthread t3 = new Sellticketsthread ("Window 3"); T1.start (); T2.start (); T3.start ();}}
Window 1 sell 10th ticket window 1 sell 9th ticket window 3 sell 8th ticket window 3 sell 7th ticket window 3 sell 6th ticket window 2 sell 5th ticket window 2 sell 4th ticket window 3 sell 3rd ticket window 1 sell 2nd Ticket
Four: Define a lock object that allows multiple threads to use the same lock together, while defining shared variables as static.
public class Sellticketsthread extends Thread {Object o =null;private static int tickets = 10;public Sellticketsthread (Str ing name,object o) {super (name); THIS.O = O;} Public Sellticketsthread () {} @Overridepublic void Run () {when (true) {while (true) {if (tickets<=0) is break; Synchronized (o) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} if (tickets>0) { System.out.println (Thread.CurrentThread (). GetName () + "sell" +tickets--+ "ticket"); }}}}}}public class Sellticketsthreaddemo {public static void main (string[] args) {object o = new Object ();//Common lock Selltick Etsthread T1 = new Sellticketsthread ("Window 1", O); Sellticketsthread t2 = new Sellticketsthread ("Window 2", O); sellticketsthread t3 = new Sellticketsthread ("Window 3", O); T1.start (); T2.start (); T3.start ();}}
Window 1 sell 10th ticket window 1 sell 9th ticket window 1 sell 8th ticket window 3 sell 7th ticket window 3 sell 6th ticket window 3 sell 5th ticket window 3 sell 4th ticket window 2 sell 3rd ticket window 2 sell 2nd Ticket
It is also important to note that the lock object of the synchronization code block is any object. So you can use object as a lock. The lock object of the synchronous method (non-static) is this. The lock object of a static method is the bytecode file object of the class.
Understanding of Java Multi-threaded ticketing Applet