When multiple threads are interacting with an object, controls need to be in place to ensure that the threads don't adversely affect one another. this chapter deals with issues that can introduce subtle errors in your application. an application that fails to safely control concurrent access can work properly most of the time-maybe nearly all the time-but will occasionally produce erroneous results. this makes the understanding and disciplined use of the information in this chapter critical to writing truly thread-safe applications that work properly all the time.
If your system encounters an inexplicable problem, it is likely to be a problem of concurrent access.
Volatile Member Variable Modifier
The Java Language Specification indicates that for optimal speed, individual threads are permitted to keep a working copy of shared member variables and only reconcile them with the shared original occasionally. to be more accurate, the word "occasionally" in the last sentence shocould be replaced with "when a thread enters or leaves a synchronized block of code. "I'll tell you more about synchronized blocks later in this chapter. when only one thread is interacting with the member variables of an object, this optimization works very well and can allow for faster execution. when two (or more) threads are simultaneously working with an object, care must be taken to ensure that changes made to a shared member variable by one thread are seen by the other.
The Java specification specifies that each thread has a copy of the shared Member variable to optimize the speed. The variable value is written back only when the thread starts or ends a synchronous code block. This mechanism works well in a single thread to achieve higher performance, but when multiple threads work together, you must consider it, public variables changed by one thread should be known by other threads.
The volatile keyword is used as a modifier on member variables to force individual threads to reread the variable's value from shared memory every time the variable is accessed. in addition, individual threads are forced to write changes back to shared memory as soon as they occur. this way, two different threads always see the same value for a member variable at any participant time. chances are that most of you expected this behavior from the Java VM already. in fact, export experienced Java developers don't understand when the use of volatile is necessary.
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