Some understandings of synchronized (this)

xiaoxiao2021-03-06  39

SYNCHRONIZED (this) means:

First, when two concurrent threads accesses this Synchronized (this) synchronous code block in the same object Object, only one thread can be implemented in a time. Another thread must wait for the code block after the previous thread is executed.

Second, however, when a thread accesses an SYNCHRONIZED (THIS) synchronous code block of Object, the other thread can still access the non-Synchronized (THIS) synchronization code block in the object.

Third, especially the key is that when a thread accesses an SYNCHRONIZED (THIS) synchronous code block, other threads will be blocked.

Fourth, the third example also applies other synchronous code blocks. That is to say, when a thread accesses an SYNCHRONIZED (THIS) synchronous code block, it gets this object lock. As a result, other threads have access to all synchronous code parts of the Object object to be temporarily blocked.

5. The above rules apply the same to other object locks.

for example:

First, when two concurrent threads accesses this Synchronized (this) synchronous code block in the same object Object, only one thread can be implemented in a time. Another thread must wait for the code block after the previous thread is executed.

Package ths;

public class Thread1 implements Runnable {public void run () {synchronized (this) {for (int i = 0; i <5; i ). {System.out.println (Thread.currentThread () getName () "synchronized loop " i);}}}} public static void main (string [] args) {thread1 t1 = new thread1 (); thread ta = new thread (t1," a "); thread TB = New Thread (T1," B "); Ta.start (); tb.Start ();}}

result:

A synchronized loop 0A synchronized loop 1A synchronized loop 2A synchronized loop 3A synchronized loop 4B synchronized loop 0B synchronized loop 1B synchronized loop 2B synchronized loop 3B synchronized loop 4

Second, however, when a thread accesses an SYNCHRONIZED (THIS) synchronous code block of Object, the other thread can still access the non-Synchronized (THIS) synchronization code block in the object.

Package ths;

Public class thread2 {public void m4t1 () {INCHRONIZED (this) {INT i = 5; while (i-> 0) {system.out.println (thread.currentthread (). getname () ":" i ); Try {thread.sleep (500);} catch (interruptedException IE) {}}}} public void m4t2 () {INT i = 5; While (I-> 0) {system.out.println (thread. CurrentThread (). getName () ":" i); try {thread.sleep (500);} catch (interruptedException IE {}}} public static void main (string [] args) {Final Thread2 myt2 = new Thread2 (); thread t1 = new thread (new ruid Run () {myt2.m4t1 ();}}, "t1"); thread t2 = new thread (new runnable () {public void Run () {myt2.m4t2 ();}}, "t2"); T1.Start (); t2.start ();}}

T1: 4T2: 4T1: 3T2: 3T1: 2T2: 2T1: 1T2: 1T1: 0T2: 0

Third, especially the key is that when a thread accesses an SYNCHRONIZED (THIS) synchronous code block, other threads will be blocked.

// Modify the Thread2.m4t2 () method:

Public void M4T2 () {INCHRONIZED (this) {INT i = 5; While (i-> 0) {system.out.println (thread.currentthread (). getname () ": i); try { Thread.sleep (500);} catch (interruptedException IE) {}}}}

T1: 4T1: 3T1: 2T1: 1T1: 0T2: 4T2: 3T2: 2T2: 1T2: 0

Fourth, the third example also applies other synchronous code blocks. That is to say, when a thread accesses an SYNCHRONIZED (THIS) synchronous code block, it gets this object lock. As a result, other threads have access to all synchronous code parts of the Object object to be temporarily blocked.

// Modify the Thread2.m4t2 () method as follows:

Public synchronized void m4t2 () {INT i = 5; while (i-> 0) {system.out.println (thread.currentthread (). getName () ":" i); try {thread.sleep 500);} catch (interruptedException IE {}}}

result:

T1: 4T1: 3T1: 2T1: 1T1: 0T2: 4T2: 3T2: 2T2: 1T2: 0

5. The above rules apply to other object locks:

Package ths;

Public class thread3 {Class Inner {Private Void M4T1 () {INT i = 5; While (i-> 0) {system.out.println (thread.currentthread (). getName () ": Inner.m4t1 () = " i); try {thread.sleep (500);} catch (interruptedException IE) {}}} private void m4t2 () {INT i = 5; while (i-> 0) {system.out.println (3Read.currentthread (). Getname () ": inner.m4t2 () =" i); try {thread.sleep (500);} catch (interruptedException IE) {}}}} private void M4T1 (Inner Inner ) {Synchronized (inner) {// uses object lock inner.m4t1 ();}} private void m4t2 (inner) {inner.m4t2 ();} public static void main (string [] args) {Final Thread3 myt3 = New thread3 (); final inner inner = myt3.new inner (); Thread T1 = New Thread (New Runnable () {myt3.m4t1 (inner);}}, "t1"); thread t2 = new thread (new runnable () {public void Run () {mYt3 .m4t2 (inner);}}, "t2"); t1.start (); t2.start ();}}

Although thread T1 obtains an object lock to Inner, since the thread T2 is accessible is the non-synchronized portion in the same INNER. So the two threads do not interfere with each other.

T1: Inner.m4t1 () = 4t2: inner.m4t2 () = 4t1: inner.m4t1 () = 3t2: inner.m4t2 () = 3t1: inner.m4t1 () = 2t2: inner.m4t2 () = 2T1: Inner.m4t1 () = 1T2: Inner.m4t2 () = 1t1: Inner.m4t1 () = 0t2: inner.m4t2 () = 0 Now in Inner.m4t2 () before adding Synchronized:

Private synchronized void m4t2 () {INT i = 5; while (i-> 0) {system.out.println (thread.currentthread (). getname () ": inner.m4t2 () =" i); Try {thread.sleep (500);} catch (interruptedException IE) {}}}

result:

Although thread T1 and T2 have accessed two unrelated parts in the same INNER object, because T1 gets the object lock to INNER first, T2 is also blocked by the INNER.M4T2 (), because M4T2 () Is a synchronization method in Inner.

T1: inner.m4t1 () = 4t1: inner.m4t1 () = 3t1: inner.m4t1 () = 2t1: inner.m4t1 () = 1T1: inner.m4t1 () = 0t2: inner.m4t2 () = 4t2: Inner.m4t2 () = 3t2: inner.m4t2 () = 2t2: inner.m4t2 () = 1t2: inner.m4t2 () = 0

转载请注明原文地址:https://www.9cbs.com/read-62449.html

New Post(0)