Synchronized in Java multi-thread programming

Source: Internet
Author: User

Synchronized, as its name implies, represents thread synchronization. So how to use it in Java programming?

Let's first look at this situation: For the same variable syndemo, we call syndemo. synmethord1 () and syndemo. synmethord2 () in two different threads ().

 Package Com. cnblogs. gpcuster;

/**
*
* @ Author Aaron. Guo
*
*/ Public Class Tester {

Public Static Void Main (string [] ARGs ){
Final Syndemo = New Syndemo ();

Thread thread1 = New Thread (){

@ Override
Public Void Run (){
// Todo auto-generated method stub Super . Run ();

Syndemo. synmethord1 ();
}

};

Thread thread2 = New Thread (){

@ Override
Public Void Run (){
// Todo auto-generated method stub Super . Run ();

Syndemo. synmethord2 ();
}

};

Thread1.start ();
Thread2.start ();

While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Main ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}}

Our syndemo is defined as follows:

 Package Com. cnblogs. gpcuster;

/**
*
* @ Author Aaron. Guo
*
*/ Public Class Syndemo {

Public Void Synmethord1 (){
While (True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord1 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}} Public Void Synmethord2 (){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord2 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}}

Our syndemo object does not have any special definitions, so after running it, the situation is as follows:

Synmethord2

Synmethord1

Main

Synmethord2

Synmethord1

Main

Synmethord2

Synmethord1

Next, we add the synchronized declaration to synmethord1, and the running condition is the same as the previous one, because we only have one thread to call the synmethord1 method.

We also add the synchronized declaration to synmethord2.CodeTo:

 Package Com. cnblogs. gpcuster;

/**
*
* @ Author Aaron. Guo
*
*/ Public Class Syndemo {

Public Synchronized Void Synmethord1 (){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord1 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}} Public Synchronized Void Synmethord2 (){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord2 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}}

At this time, we will runProgramThe results are different:

Main

Synmethord1

Main

Synmethord1

Main

Synmethord1

We found that for the syndemo object, only synmethord1 is running, but synmethord2 is not. This should be because the method-level synchronized declaration will lock the current instance of this class object. Therefore, the current instance cannot run synmethord2 before synmethord1 stops unlock. Synchronized at the method level is equivalent to the following:

 Package Com. cnblogs. gpcuster;

/**
*
* @ Author Aaron. Guo
*
*/ Public Class Syndemo {

Public Void Synmethord1 (){
Synchronized ( This ){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println ("Synmethord1 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}} Public Void Synmethord2 (){
Synchronized ( This ){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord2 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}}}

Run the program and the result is the same as the previous one.

What if we want to synchronize two methods? You can refer to this implementation:

 Package Com. cnblogs. gpcuster;

/**
*
* @ Author Aaron. Guo
*
*/ Public Class Syndemo {

Private Object flag1 =New Object ();
Private Object flag2 = New Object ();

Public Void Synmethord1 (){
Synchronized (Flag1 ){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord1 ");
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}} Public Void Synmethord2 (){
Synchronized (Flag2 ){
While ( True ){
Try {
Thread. Sleep (1000 );
System. Out. println (" Synmethord2 ");
}Catch (Interruptedexception e ){
// Todo auto-generated Catch Block E. printstacktrace ();}}}}}

Run the program. The result is as expected:

Main

Synmethord2

Synmethord2

Main

Synmethord1

Main

Synmethord1

Synmethord2

 

Synchronized has other topics, such as static issues, inheritance issues, and use with volatile. They are described in detail on the Internet.

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.