Java synchronized (1)

Source: Internet
Author: User

Keyword of the Java language, when it is used to modify a method orCodeAt the same time, only one thread can execute the code at most.

1. When two concurrent threads access the synchronized (this) synchronization code block of the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block before executing this code block.

2. However, when a thread accesses a synchronized (this) synchronization code block of the object, the other thread can still access the non-synchronized (this) synchronization code block of the object.

3. When a thread accesses a synchronized (this) synchronization code block of the object, other threads perform synchronization on all other synchronized (this) access to the synchronization code block will be blocked.

4. The third example also applies to other synchronous code blocks. That is to say, when a thread accesses a synchronized (this) synchronization code block of an object, it obtains the object lock of this object. As a result, access by other threads to all the synchronized code parts of the object is temporarily blocked.

5. The above rules apply to other Object locks.

Example:
1. When two concurrent threads access the synchronized (this) synchronization code block of the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block before executing this code block.

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, "400 phone ");
Thread TB = new thread (T1, "800 phone ");
Ta. Start ();
TB. Start ();
}
}

Result:
400 phone Synchronized loop 0
400 phone Synchronized loop 1
400 phone Synchronized loop 2
400 phone Synchronized loop 3
400 phone Synchronized loop 4
800 phone Synchronized loop 0
800 phone Synchronized loop 1
800 phone Synchronized loop 2
800 phone Synchronized loop 3
800 phone Synchronized loop 4

2. However, when a thread accesses a synchronized (this) synchronization code block of the object, the other thread can still access the non-synchronized (this) synchronization code block of the object.

Package ths;

Public class thread2 {
Public void m4t1 (){
Synchronized (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 runnable () {public void run () {myt2.m4t1 () ;}, "T1 ");
Thread t2 = new thread (New runnable () {public void run () {myt2.m4t2 () ;}, "T2 ");
T1.start ();
T2.start ();
}
}

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

3. When a thread accesses a synchronized (this) synchronization code block of the object, other threads perform synchronization on all other synchronized (this) access to the synchronization code block will be blocked.

// Modify the thread2.m4t2 () method:
Public void m4t2 (){
Synchronized (this ){
Int I = 5;
While (I --> 0 ){
System. Out. println (thread. currentthread (). getname () + ":" + I );
Try {
Thread. Sleep (500 );
} Catch (interruptedexception IE ){
}
}
}

}

Result:

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

4. The third example also applies to other synchronous code blocks. That is to say, when a thread accesses a synchronized (this) synchronization code block of an object, it obtains the object lock of this object. As a result, access by other threads to all the synchronized code parts of the object is temporarily blocked.

// Modify thread2.m4t2 () 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) {
}< BR >}

Result:
T1: 4
T1: 3
T1: 2
T1: 1
T1: 0
T2: 4
T2: 3
T2: 2
T2: 1
T2: 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 (thread. currentthread (). getname () + ": inner. m4t2 () =" + I );
Try {
Thread. Sleep (500 );
} Catch (interruptedexception IE ){
}
}
}
}
Private void m4t1 (inner ){
Synchronized (inner) {// use the object lock
Inner. m4t1 ();
}

Private void m4t2 (inner ){
Inner. m4t2 ();
}

Public static void main (string [] ARGs ){
Final thread3 myt3 = new thread3 ();
Final inner = myt3.new inner ();
Thread T1 = new thread (New runnable () {public void run () {myt3.m4t1 (inner) ;}, "T1 ");
Thread t2 = new thread (New runnable () {public void run () {myt3.m4t2 (inner) ;}, "T2 ");
T1.start ();
T2.start ();
}
}

 

Result:

Although thread T1 acquires the object lock on the inner, thread T2 accesses the non-synchronous part of the same inner. Therefore, the two threads do not interfere with each other.

T1: inner. m4t1 () = 4
T2: inner. m4t2 () = 4
T1: inner. m4t1 () = 3
T2: inner. m4t2 () = 3
T1: inner. m4t1 () = 2
T2: inner. m4t2 () = 2
T1: inner. m4t1 () = 1
T2: inner. m4t2 () = 1
T1: inner. m4t1 () = 0
T2: inner. m4t2 () = 0

Now add synchronized before inner. m4t2:

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) {
}< BR >}

Result:

Although thread t1 and t2 access two unrelated parts of the same inner object, because T1 first obtains the object lock on inner, so T2 has access to inner. access to m4t2 () is also blocked because m4t2 () is a synchronous method in the inner.

T1: inner. m4t1 () = 4
T1: inner. m4t1 () = 3
T1: inner. m4t1 () = 2
T1: inner. m4t1 () = 1
T1: inner. m4t1 () = 0
T2: inner. m4t2 () = 4
T2: inner. m4t2 () = 3
T2: inner. m4t2 () = 2
T2: inner. m4t2 () = 1
T2: inner. m4t2 () = 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.