This article mainly introduces relevant information about the detailed explanation of the synchronize function in Java. I hope this article can help everyone understand how to use the synchronize function. Friends in need can refer to it
Detailed explanation of examples of synchronize function in Java
If a member function of a class in Java is modified with synchronized, it corresponds to the same object. Multiple threads must wait until they call the synchronization function of this object. The next thread can only call it after the previous thread has called it.
So if a class has two member functions that are modified by synchronized as shown in the code, for the same object, can one call funcA and the other call funcB when two threads are running?
Mysyn is such a class. If I have two threads, one runs funcA first and then funcB in the run method, and the other thread runs funcB first and then funcA in the run method. Is it possible that when outputting start A... then directly output start B...?
public class MySyn { public synchronized void funcA(String str){ System.out.println(str+":"); System.out.println("start A..."); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("...A end"); } public synchronized void funcB(String str){ System.out.println(str+":"); System.out.println("start B..."); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("...B end"); } }
The test code is as follows:
This thread runs funcA first
public class Mythread implements Runnable { private MySyn mysyn; private String id; public Mythread(MySyn syn, String id){ this.mysyn = syn; this.id = id; } @Override public void run() { this.mysyn.funcA(id); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.mysyn.funcB(id); } public static void main(String arg[]){ MySyn syn=new MySyn(); Thread t1 = new Thread(new Mythread(syn, "t1")); Thread t2 = new Thread(new YourThread(syn, "t2")); t1.start(); t2.start(); } }
This The thread runs funcB first
public class YourThread implements Runnable { private MySyn mysyn; private String id; public YourThread(MySyn syn, String id){ this.mysyn = syn; this.id=id; } @Override public void run() { this.mysyn.funcB(id); this.mysyn.funcA(id); } }
The output result is mostly:
t1: start A... ...A end t2: start B... ...B end t2: start A... ...A end t1: start B... ...B end
If you cancel the run method of Mythread Sleep between two function calls, the result is probably:
t1: start A... ...A end t1: start B... ...B end t2: start B... ...B end t2: start A... ...A end
Individual results may be different due to thread scheduling, but there will never be: start A...directly afterwards Output start B...
If funcB is not a synchronous function, what will be the result of the above code?
The code is slightly modified to remove the synchronized keyword of funcB. The running result is:
t2: t1: start A... start B... ...A end t1: start B... ...B end t2: start A... ...B end ...A end
Obviously the result of start A... is directly output after start B....
Similarly, if the Mysyn class has a public member variable, multi-threads can also modify the member variable by another thread while the synchronization function is being called.
The above experiment illustrates: Synchronized member functions can only have an exclusive effect on other synchronized functions (including itself) in the synchronized function call of the same object. That is, during multi-thread running, the same object is currently Only one synchronized function can be running, but this does not exclude other non-synchronized functions from running or accessing members.
Now suppose a class has two static synchronization methods, what about the situation?
I will not repeat the specific implementation, because the results are similar:
In multi-threading, the same class can currently only have one class synchronization function (static synchronization function) running. But it does not exclude the execution of other non-synchronized static functions or access to static members
The above is the detailed content of Example sharing of synchronize function in Java. For more information, please follow other related articles on the PHP Chinese website!