Synchronized keyword in Java

Source: Internet
Author: User

Today, I went to javaworld.com.tw and saw someone asking about thread synchronization. Recently I am very interested in multithreading and have written many similar programs. So thread synchronization is definitely required, synchronized is used in the middle of the function name. I remember to synchronize objects. That is to say, if multiple threads want to maintain synchronization, they must ensure that these threads use an object. I have seen synchronized write in the function, but I don't have to feel it. I tested it today and wrote synchronized to the function, so there is no need to guarantee an object. All n different objects can be used. This is the code.

First, write synchronized into the function.

/** <Br/> */<br/> package COM. baseramework. core. thread; </P> <p>/** <br/> * thread test <br/> * @ author sunyanan <br/> * /<br/> public class threadtest {</P> <p> Public static void main (string [] ARGs) {<br/> thread T1 = new thread (New executethread (); <br/> thread t2 = new thread (New executethread ()); <br/> t1.start (); <br/> t2.start (); <br/>}</P> <p>/** <br/> * task class, to simulate our task <br/> * @ author sunyanan <br/> */<br/> class taskclass {</P> <p> private taskclass () {}; </P> <p> Private Static taskclass T = new taskclass (); </P> <p> Public static taskclass getinstance () {<br/> return t; <br/>}</P> <p> Public synchronized void printdata () {<br/> system. out. print (thread. currentthread (). getname () + ":"); <br/> for (INT I = 0; I <100; I ++) {<br/> system. out. print (I + ""); <br/>}< br/> system. out. println (); <br/>}</P> <p>/** <br/> * Thread class <br/> * @ author sunyanan <br /> * <br/> */<br/> class executethread implements runnable {<br/> taskclass T = taskclass. getinstance (); <br/> @ override <br/> Public void run () {<br/> T. printdata (); <br/>}</P> <p >}< br/>

Running result:

We can see that the two threads are synchronized,

Let's take a look at the keywords written internally:

 

/** <Br/> */<br/> package COM. baseramework. core. thread; </P> <p>/** <br/> * thread test <br/> * @ author sunyanan <br/> * /<br/> public class threadtest {</P> <p> Public static void main (string [] ARGs) {<br/> thread T1 = new thread (New executethread (); <br/> thread t2 = new thread (New executethread ()); <br/> t1.start (); <br/> t2.start (); <br/>}</P> <p>/** <br/> * task class, to simulate our task <br/> * @ author sunyanan <br/> */<br/> class taskclass {</P> <p> Public void printdata () {<br/> synchronized (taskclass. class) {<br/> system. out. print (thread. currentthread (). getname () + ":"); <br/> for (INT I = 0; I <100; I ++) {<br/> system. out. print (I + ""); <br/>}< br/> system. out. println (); <br/>}</P> <p>/** <br/> * Thread class <br/> * @ author sunyanan <br/> */<br/> class executethread implements runnable {<br/> taskclass T = new taskclass (); <br/> @ override <br/> Public void run () {<br/> T. printdata (); <br/>}</P> <p >}< br/>

 

The running result is the same as above, so I don't have much.

 

In comparison, the code for writing keywords to the function declaration, but not an object:

 

/** <Br/> */<br/> package COM. baseramework. core. thread; </P> <p>/** <br/> * thread test <br/> * @ author sunyanan <br/> * /<br/> public class threadtest {</P> <p> Public static void main (string [] ARGs) {<br/> thread T1 = new thread (New executethread (); <br/> thread t2 = new thread (New executethread ()); <br/> t1.start (); <br/> t2.start (); <br/>}</P> <p>/** <br/> * task class, to simulate our task <br/> * @ author sunyanan <br/> */<br/> class taskclass {</P> <p> Public synchronized void printdata () {<br/> system. out. print (thread. currentthread (). getname () + ":"); <br/> for (INT I = 0; I <100; I ++) {<br/> system. out. print (I + ""); <br/>}< br/> system. out. println (); <br/>}</P> <p>/** <br/> * Thread class <br/> * @ author sunyanan <br /> * <br/> */<br/> class executethread implements runnable {<br/> taskclass T = new taskclass (); <br/> @ override <br/> Public void run () {<br/> T. printdata (); <br/>}</P> <p >}< br/>

Here is the result:

We can see that there is no synchronization. This makes it clear that the synchronization is out of the box ..

 

PS: I will start the test tomorrow ..

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.