Java thread mutex is to ensure that at most one thread at a time executes the code. So what's the problem with its appearance? Account access funds can only be operated by one person at the same time period.
Let's look at a simple example (the problem with multithreading):
public class Traditionalthreadsynchronized {/** * @param args */public static void main (string[] args) {new traditionalthr Eadsynchronized (). init ();} private void Init () {///This method starts two threads at the same time to call the same method's print name final outputer outputer = new Outputer (); new Thread (new Runnable () {@ overridepublic void Run () {while (true) {try {thread.sleep];} catch (Interruptedexception e) {e.printstacktrace ();} Outputer.output ("Zhangxiaoxiang");}}). Start (); New Thread (New Runnable () {@Overridepublic void run () {while (true) {try {Thread.Sleep]} 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<len;i++) {System.out.print (Name.charat (i));} System.out.println ();/}}}}
Printing results are:
To solve the above problems:
Add synchronized keywords, that is, to extract the comments.
Printing results:
Summarize:
When two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block. That is, when a thread accesses one of the synchronized (this) synchronization blocks of object, other threads will block access to all other synchronized (this) synchronized code blocks in object.
Note: To be mutually exclusive, you must make the lock the same. In the demo above , two threads are using the same new output, so output is the same object.
For more information, see blog [Original]02____ thread synchronization (Synchronized)
Brief answer:
public class Test implements Runnable {public int cash = +; public synchronized void M () { System.out.println ("M view account, Balance is" +cash); try { thread.sleep; } catch (Interruptedexception e) { e.printstacktrace (); } Cash = cash-100; System.out.println ("CASH1 =" + Cash); } Public synchronized void Run () { System.out.println ("Run View account, balance is" +cash); Cash + + +; try { thread.sleep; } 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 that after you add 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 have to execute this method before other methods are executed. All two methods must have the Synchronized keyword, and both lock the same object (the object locked here is the test object). That is, as long as a thread enters any lock method on the test object, the other thread cannot access the object by adding the same lock method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JAVA concurrent Programming-traditional thread mutex technology (Synchronized) (iii)