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 ..