Variable data analysis _java in-depth synchronization access sharing

Source: Internet
Author: User
If access to shared mutable data is not synchronized, the consequences are dire, even if the variable is read and written to the atom.
Consider a thread synchronization issue below. For thread synchronization, the Java class Library provides a thread.stop approach, but this method is not worth advocating because it is inherently unsafe. It's better to use polling (Polling), such as the following procedure.
Copy Code code as follows:

Import Java.util.concurrent.TimeUnit;
public class Stopthread {
/**
* @param args
*/

private static Boolean stoprequested;

public static void Main (string[] args)
Throws interruptedexception{

Thread backgroundthread = new Thread (new Runnable () {

@Override
public void Run () {

int i = 0;
while (!stoprequested) {
i++;
System.out.println (i);
}
}
});
Backgroundthread.start ();
TimeUnit.SECONDS.sleep (1);
Stoprequested = true;
}
}

You may think that the program is running for about a second, because the main thread has set the stoprequested to true, so that the new thread in the background to stop, actually, because the background thread can not see this value changes, so will continue to the wireless loop, which is not synchronized with the consequences of the data. So let's implement this task in a synchronized way.
Copy Code code as follows:

Import Java.util.concurrent.TimeUnit;
public class Stopthread {
/**
* @param args
*/

private static Boolean stoprequested;

private static synchronized void RequestStop () {
Stoprequested = true;
}
private static Synchronized Boolean stoprequested () {
return stoprequested;
}

public static void Main (string[] args)
Throws interruptedexception{

Thread backgroundthread = new Thread (new Runnable () {

@Override
public void Run () {

int i = 0;
while (!stoprequested ()) {
i++;
System.out.println (i);
}
}
});
Backgroundthread.start ();
TimeUnit.SECONDS.sleep (1);
RequestStop ();
}
}

This enables the synchronization of data, and it is noteworthy that both the Write method (RequestStop) and the Read method (stoprequested) need to be synchronized, otherwise they are still not in the true sense of synchronization.
In addition, we can use the variable modifier volatile to more simply complete the synchronization task.
Copy Code code as follows:

import Java.util.concurrent.TimeUnit;
public class Stopthread {
 /**
  * @param args
  * *

 private static Vola Tile Boolean stoprequested;

 public static void Main (string[] args)
  throws interruptedexception{

 &nb Sp Thread backgroundthread = new Thread (new Runnable () {

    @Override
   public void Run () {

    int i = 0;
    while (!stoprequested) {
     i++;
     system.out.println (i);
    }
   }
  });
  backgroundthread.start ();
  timeunit.seconds.sleep (1);
  stoprequested = true;
 }
}

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.