Multi-threaded concurrency brings many benefits to our programming and completes more efficient programs. But it also poses a thread-safety issue for us.
The key to solving the problem is to ensure the atomicity of the code that is prone to problems, so-called atomicity means: when a thread executes a piece of code, another thread must wait until the a thread executes, before it can execute the code. that is, queuing up one solution.
The Java approach to threading two steps is very simple, just need to be synchronized in the code snippet, with:synchronized (Object) {You want to synchronize the code} can.
Explanation of the synchronization mechanism:
Any type of Java object has a flag bit with 0, 12 states, with a starting state of 1, and when a thread executes the synchronized (object) statement, the object's flag bit becomes a 0 state. Until the code block in the complete synchronized statement is executed, the object's flag bit returns to the 1 state.
when a thread executes to a synchronized (object) statement, it first checks the object's flag bit, and if it is a 0 state, indicating that another thread is executing the code included in the synchronized, the thread will temporarily block, Make up the CPU resource until another thread executes the associated synchronization code, and the object object's flag bit becomes state, the thread's blocking is canceled, the thread continues to run, and the thread changes the flag bit of object to 0 state, preventing other threads from entering the associated synchronization code block.
If multiple threads are blocked while waiting for the same object's flag face, when the object's flag bit is restored to the 1 state, only one thread will be able to enter synchronous code execution, while the other threads remain in a blocked state.
Special Note:
1, the above-mentioned signs are used in terms of the object lock, file lock. The database will have row locks, table locks, etc.
2, synchronized (object)//object (that is, objects lock) can be any type of object
3 synchronized can also be modified by the method, and when it is decorated, the object lock defaults to (this).
Demo of ticketing system
1/**2*Features: Considerations for using Threads3*use of thread concurrency Sync lock synchronized (Object) {}4*/5 Public classTHREAD05 {6 Public Static voidMain (string[] args) {7 //define a ticket window8Ticketwindow tw1=NewTicketwindow ();9 Ten //start at the same time with three threads OneThread t1=NewThread (TW1); AThread t2=NewThread (TW1); -Thread t3=NewThread (TW1); - the T1.start (); - T2.start (); - T3.start (); - } + } - + //Ticket window class A classTicketwindowImplementsRunnable { at //Total 2000 Tickets - Private intnums=2000; - PrivateDog mydog=NewDog (); - - Public voidrun () { - while(true){ in //Ticketing speed is 1 seconds out of one - Try { toThread.Sleep (1000); +}Catch(Exception e) { - e.printstacktrace (); the } * $ //consider if else to guarantee its atomicityPanax Notoginseng //decide if there's a ticket . - synchronized(Mydog) {//synchronized (this) {} is a synchronous block of code the if(nums>0){ + //Show Ticketing Information A //Thread.CurrentThread (). GetName () Gets the name of the current thread theSystem.out.println (Thread.CurrentThread (). GetName () + "+nums+" ticket sold); +nums--; -}Else{ $ //Ticket End $ Break; - } - } the } - }Wuyi}
The threading problem in Java is (iv)--thread synchronization issues