Java Concurrency Programming Supplement-thread visibility

Source: Internet
Author: User

Define a class Visibility1 contains two variables

public class Visibility1 {
public static Boolean ready;
public static int number;
}
Customize a thread class to enter a loop when the Ready property in Visibility1 is False
public class Readerthread extends thread{

@Override
public void Run () {
while (! Visibility1.ready) {
Thread.yield ();
System.out.println (Visibility1.number);
}
}
}
Test thread
 public class TestThread1 {
public static void Main (string[] args) {
New Readerthread (). Start ();
Visibility1.number = 42;
Visibility1.ready = true;
}
}
There are three output results:
0: This is the case of first entering the loop to perform a single output;
42: This is the loop that is entered after number is assigned a value of 42;
Null: This is the case that ready is directly assigned to True, Does not enter the loop;
There is a problem with ordering the instruction, for example: int a = 1; int b = 2; int c = A+B; This three statement CPU is likely to execute the second, but not first, (CPU optimization means that the second execution time is shorter than the first one).

Here is a test class visibility which has a bchanged variable, two infinite loop threads, which, in theory, will stop the program, but the execution result does not stop
public class Visibility {

private static Boolean bchanged;

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

/**
* The first thread, when bchanged is true, outputs "! =" and then exits the Java Virtual machine
*/
New Thread ((), {
for (;;) {
if (bchanged = = True) {
SYSTEM.OUT.PRINTLN ("! =");
System.exit (0);
}
}
}). Start ();

/**
* Second thread, assign this time, bchanged to True
*/
Thread.Sleep (10);
New Thread ((), {
for (;;) {
Bchanged = true;
}
}). Start ();
}
}
For example, because the CPU is running too fast and there is a layer of cache between CPU memory, entering the dead loop does not exit because the first thread's variable is the read cache.

Workaround: Use the volatile keyword: 1 to ensure that variables are modified so that all threads are visible; 2 block instruction ordering;

Volatile is the older keyword synchroized has been optimized very well, do not deliberately use volatile

private static volatile Boolean bchanged;

Sync can solve the visibility, atomic volatile can only solve if the CAS operation is not atomized multi-threaded operation there must be a problem first compare the CAS operations in the Modify compare and set

Thread concurrency problem: Write or read an outdated data, solution

1. Thread closure: Final do not share variables between threads (about equal to useless)

2. Stack closure: For example, the method internal declaration, the modification does not overflow, the method executes the release, does not exist in the memory

3.ThreadLocal thread binding.

 

Java Concurrency Programming Supplement-thread visibility

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.