We all know that traditional thread synchronization can be implemented through synchronous code blocks or synchronous methods.
The synchronization monitor is involved here.
If you do not know about the synchronization monitor to get the thread-related knowledge is not understanding, you can view the blog http://blog.csdn.net/johnny901114/article/details/7536951
To ensure synchronization between threads, the monitor object is the same object for Synchronous threads, so as to achieve synchronization mutex effect.
Therefore, many users use the most synchronous object of the bytecode (class), because only one part of the bytecode of A Class exists in the memory. of course this is good when it comes to a set of synchronous functions.
What if there are multiple groups of functions? Since there are multiple groups, there will be multiple synchronization monitor objects. If the synchronization monitor uses the bytecode of the same class, it will become a set of synchronization functions.
In the following example, two sets of synchronous function headers synchronize and mutually excludes print (), and the latter two threads synchronize and mutually excludes printname; that is, the two groups have different responsibilities. does not affect each other.
Public synchronized static void print () {// synchronized (threadgroup. class) {string source = "I am a Chinese"; char [] cs = source. tochararray (); system. out. println (thread. currentthread (). getname (); For (Int J = 0; j <30; j ++) {system. out. println (J + "----------------->"); For (INT I = 0; I <CS. length; I ++) {system. out. print (CS [I]);} system. out. println () ;}///} public synchronized static void printname () {// synchronized (string. class) {system. out. println (thread. currentthread (). getname (); string source = "hello"; char [] cs = source. tochararray (); For (Int J = 0; j <30; j ++) {system. out. println (J + "----------------->"); For (INT I = 0; I <CS. length; I ++) {system. out. print (CS [I]);} system. out. println ();}//}}
Public static void main (string [] ARGs) {New thread (New runnable () {@ overridepublic void run () {print ();}}). start (); New thread (New runnable () {@ overridepublic void run () {print ();}}). start (); // -------------------------- the second group is new thread (New runnable () {@ overridepublic void run () {printname ();}}). start (); New thread (New runnable () {@ overridepublic void run () {printname ();}}). start ();}
Because print () and printname () are both static and modified with the synchronized keyword. so the synchronization monitor object is the bytecode of the current class. in fact, there is only one set of synchronization functions. instead of two groups
The output result shows that when a thread executes the print () or printname () method, the other three threads can only be executed after the thread completes execution.
If multiple groups exist, the synchronization mutex of one group does not affect the execution of other groups.
Because there are many output results, do not paste them.
Change the print () and printname () Methods to the following: the two groups are synchronized.
Public static void print () {synchronized (threadgroup. class) {string source = "I am a Chinese"; char [] cs = source. tochararray (); system. out. println (thread. currentthread (). getname (); For (Int J = 0; j <30; j ++) {system. out. println (J + "----------------->"); For (INT I = 0; I <CS. length; I ++) {system. out. print (CS [I]);} system. out. println () ;}} public static void printname () {synchronized (string. class) {system. out. println (thread. currentthread (). getname (); string source = "hello"; char [] cs = source. tochararray (); For (Int J = 0; j <30; j ++) {system. out. println (J + "----------------->"); For (INT I = 0; I <CS. length; I ++) {system. out. print (CS [I]);} system. out. println ();}}}
The output result shows that the two groups of synchronization are executed alternately.
Reprinted please indicate the source: http://blog.csdn.net/johnny901114/article/details/7854666
Please kindly advise if you have any mistakes. Thank you!