Understand Synchronized (Object) lock

xiaoxiao2021-03-06  14

Package test;

Public class test2 {static class inner1 {public void m1 () {synchronized ("aaaa") {for (CHAR C = 'a'; c <= 'g'; c ) {system.out.println (Thread.currentThread " ) .getname () "m1 ():" c); try {thread.sleep (100);} catch (interruptedException e) {}}}} public synchronized void m2 () {for (char C = 'a '; c <=' g '; c ) {system.out.println (thread.currentthread (). getname () "m2 ():" c); try {thread.sleep (100);} catch InterruptedException E) {}}}} public static void main (string [] args) {final inner1 in1 = new inner1 (); thread t1 = new thread (new ruid Run () {public void Run () {in1.m1 () } }, "T1"); thread t2 = new thread (new ruid run () {public void Run () {in1.m2 ();}}, "t2"); t1.start (); t2.start (); }

result:

T1 M1 (): AT2 M2 (): AT1 M1 (): BT2 M2 (): BT1 m1 (): CT2 M2 (): CT1 M1 (): DT2 M2 (): DT1 M1 (): ET2 M2 (): ET1 M1 (): ft2 m2 (): ft1 m1 (): gt2 m2 (): g

analysis:

M2 () is a synchronization method in the Inner1 class, and M1 () contains a synchronization block.

The synchronization block in M1 () is only synchronized for the object "AAAA", which cannot be synchronized with the Inner1 instance itself. Therefore, as shown in the above example, the two threads do not interfere with each other and run.

Look at the following example: package test;

Public class test1 {static class inner1 {pUBLIC VOID M1 (Inner2 I2) {string str = thread.currentthread (). getname (); synchronized (i2) {system.out.println (STR "ENTER: inner1.m1 () "); For (char c = 'a'; c <= 'g'; c ) {system.out.println (STR " goto: " c); try {thread.sleep (100);} catch InterruptedException E) {}} system.out.println (Str "Leave: inner1.m1 ()");}}}} public synchronized void m2 () {string str = thread.currentthread (). Getname (); system.out .println (STR "ENTER: INNER1.M2 ()"); for (CHAR C = 'a'; c <= 'z'; c ) {system.out.println (Str "goto:" c) ; Try {thread.sleep (100);} catch (interruptedexception e) { }}} System.out.println (Str "Leave: Inner1.m2 ()");}}}}}}}}}}}}}} static class inner2 {String str = thread.currentthread (). GetName (); system.out .println (STR "ENTER: INNER2.M1 ()"); for (CHAR C = 'a'; c <= 'g'; c ) {system.out.println (STR "goto:" C) Try {thread.sleep (100);

} Catch (InterruptedException E) {}} system.out.println (Str "Leave: Inner2.m1 ()");}}}}}} public static void main (string [] args) {Final Inner1 in1 = new inner1 (); Final inner2 in2 = new inner2 (); thread t1 = new thread (new runnable () {public void run () {in1.m1 (in2);}}, "t1"); Thread T2 = new thread (New Runnable ) {Public void Run () {in1.m2 ();}}, "t2"); thread t3 = new thread (new runnable () {public void run () {in2.m1 ();}}, "T3 "); T1.start (); t2.start (); t3.Start ();}}

T1 enter: Inner1.m1 () T1goto: aT2 enter: Inner1.m2 () T2goto: aT1goto: bT2goto: bT1goto: cT2goto: cT1goto: dT2goto: dT1goto: eT2goto: eT2goto: fT1goto: fT2goto: gT1goto: gT2goto: hT1 leave: Inner1 . m1 () T3 ENTER: INNER2.M1 () T3GOTO: AT2GOTO: IT3GOTO: BT3GOTO: CT2GOTO: JT3GOTO: DT2GOTO: KT3GOTO: ET2GOTO: LT3GOTO: ET2GOTO: MT3GOTO: GT2GOTO: NT3 Leave: ot2goto: NT3 Leave: OT2GOTO: PT2GOTO: RT2GOTO: RT2GOTO: St2goto: TT2GOTO: UT2GOTO: VT2GOTO: WT2GOTO: XT2GOTO: YT2GOTO: ZT2 Leave: Inner1.m2 ()

analysis:

The execution of T2 runs through the entire process. T3 is called inner2.m1 () after the call to Inner1.m1 (), because the synchronization block in the Inner1.m1 () method locked the object IN2 of Inner2.

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

New Post(0)