JAVA concurrent programming-Synchronized (3)
Java threads are mutually exclusive to ensure that only one thread can execute the code segment at a time. So What problems does it solve? Only one person can perform operations during the same period of time.
Here is a simple example (the problem caused by multithreading ):
Public class TraditionalThreadSynchronized {/*** @ param args */public static void main (String [] args) {new TraditionalThreadSynchronized (). init ();} private void init () {// This method starts two threads at the same time to call the Print Name of the same method final Outputer outputer = new Outputer (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {try {Thread. sleep (10);} catch (InterruptedException e) {e. printStackTrace ();} outputer. output (zhangxiaoxiang );}}}). start (); new Thread (new Runnable () {@ Overridepublic void run () {while (true) {try {Thread. sleep (10);} catch (InterruptedException e) {e. printStackTrace ();} outputer. output3 (lihuoming );}}}). start ();} static class Outputer {public void output (String name) {int len = name. length (); // synchronized (Outputer. class) // {for (int I = 0; I
The output is as follows:
Solve the above problems:
Add the keywords of synchronized, that is, just uncomment it.
Print result:
Summary:
When two concurrent threads access the synchronized (this) synchronization code block of the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block before executing this code block. That is, when a thread accesses a synchronized (this) synchronization code block of the object, access by other threads to all other synchronized (this) synchronization code blocks of the object will be blocked.
Note: To ensure mutual exclusion, the lock must be the same. In the preceding demo, both threads use the same new output, so the output is the same object.
For more information, see [original] 02 ____ thread synchronization (Synchronized)
Brief answer:
Public class Test implements Runnable {public int cash = 100; public synchronized void m () {System. out. println (m view account, balance + cash); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} cash = cash-100; System. out. println (cash1 = + cash);} public synchronized void run () {System. out. println (run view account, balance + cash); cash + = 1000; try {Thread. sleep (5000);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (cash2 = + cash);} public static void main (String [] args) {Test test = new Test (); Thread thrad = new Thread (test); thrad. start (); test. m ();}}
You can see after adding the synchronized keyword. As long as m or run operates on the account, no matter how long it takes or how long it takes to sleep, the thread will execute this method before executing other methods. Both methods must add the synchronized keyword and lock the same object (the object locked here is the test object ). That is to say, as long as a thread enters any of the test object's locking methods, other threads will not be able to access this object using the same lock method.