Thread Synchronization reason and Method

Source: Internet
Author: User

We know that using the runnable method to implement threads can achieve resource sharing, but there are still problems. refer to the following code:

// Reason for synchronization <br/> class mythread implements runnable <br/>{< br/> private int ticket = 10; <br/> Public void run () {<br/> for (INT I = 0; I <100; I ++) {<br/> If (ticket> 0) {<br/> try {<br/> thread. sleep (2000); // when this statement is added, there will be a delay, resulting in the resource exceeding the usage range <br/>} catch (interruptedexception e) {}< br/> system. out. println (thread. currentthread (). getname () + "ticket selling:" + ticket --); <br/>}< br/> public class syncdemo01 <br/>{< br/> Public static void main (string ARGs []) {<br/> mythread my = new mythread (); <br/> thread a = new thread (my, ""); <br/> thread B = new thread (my, "B"); <br/> thread c = new thread (my, "C"); <br/>. start (); <br/> B. start (); <br/> C. start (); <br/>}< br/>}

 

When there is a delay, it may cause resource sharing errors. So we need to use synchronization to solve this problem. There are two ways to implement synchronization: Use the synchronized code block and the synchronized method.

First, let's look at the first method, using the synchronized code block:

// Use synchronized to synchronize code blocks <br/> class mythread implements runnable <br/>{< br/> private int ticket = 10; <br/> Public void run () {<br/> for (INT I = 0; I <100; I ++) {</P> <p> synchronized (this) {// Synchronous Code block <br/> If (ticket> 0) {<br/> try {<br/> thread. sleep (500); // when this statement is added, there will be a delay, resulting in the resource exceeding the usage range <br/>} catch (interruptedexception e) {}< br/> system. out. println (thread. currentthread (). getname () + "ticket selling:" + ticket --); <br/>}< br/> public class syncdemo02 <br/>{< br/> Public static void main (string ARGs []) {<br/> mythread my = new mythread (); <br/> thread a = new thread (my, ""); <br/> thread B = new thread (my, "B"); <br/> thread c = new thread (my, "C"); <br/>. start (); <br/> B. start (); <br/> C. start (); <br/>}< br/>}

We can see through running that the problem can be well solved. At the same time, you can view the preemption and switching between processes through multiple running.

Let's take a look at the second implementation method, using the synchronized method:

// Use the Synchronized Method for synchronization <br/> class mythread implements runnable <br/>{< br/> private int ticket = 10; <br/> Public void run () {<br/> for (INT I = 0; I <100; I ++) {<br/> This. fun (); <br/>}< br/> Public synchronized void fun () {<br/> If (ticket> 0) {<br/> try {<br/> thread. sleep (500); // when this statement is added, there will be a delay, resulting in the resource exceeding the usage range <br/>} catch (interruptedexception e) {}< br/> system. out. println (thread. currentthread (). getname () + "ticket selling:" + ticket --); <br/>}< br/> public class syncdemo03 <br/>{< br/> Public static void main (string ARGs []) {<br/> mythread my = new mythread (); <br/> thread a = new thread (my, ""); <br/> thread B = new thread (my, "B"); <br/> thread c = new thread (my, "C"); <br/>. start (); <br/> B. start (); <br/> C. start (); <br/>}< br/>}

Through running, you can find that this method can also solve the problem. When this is done, I have a question: if you use the Synchronized Method for synchronization, the run method is also a method. Can you set the run method to a synchronous method. To solve this problem, try the following:

// Set the run method as the synchronization method <br/> class mythread implements runnable <br/>{< br/> private int ticket = 10; <br/> Public synchronized void run () {<br/> for (INT I = 0; I <100; I ++) {<br/> If (ticket> 0) {<br/> try {<br/> thread. sleep (1000); // when this statement is added, there will be a delay, resulting in the resource exceeding the usage range <br/>} catch (interruptedexception e) {}< br/> system. out. println (thread. currentthread (). getname () + "ticket selling:" + ticket --); <br/>}</P> <p >}< br/> public class syncdemo04 <br/>{< br/> Public static void main (string ARGs []) {<br/> mythread my = new mythread (); <br/> thread a = new thread (my, ""); <br/> thread B = new thread (my, "B"); <br/> thread c = new thread (my, "C"); <br/>. start (); <br/> B. start (); <br/> C. start (); <br/>}< br/>}

If it is still successful after running, is there a problem? What are the differences between the synchronized code block and the synchronized method? The above code is a thread implemented by implementing the runnable interface. Can the thread implemented by inheriting the thread be synchronized?

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.