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.
如果你的系统出现了莫名其妙的问题,很有可能是并发访问的问题哦
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 should 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.
Java规范中说明,为了优化速度,每个线程都拥有共享成员变量的一个拷贝,当线程开始或结束一个同步代码块时,才回写变量值。当单线程时,这种机制很好的工作,可以获得更高的性能,但是多个线程协同工作时,必须考虑,被一个线程改变的公共变量要被其他线程知道。
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 particular time. Chances are that most of you expected this behavior from the Java VM already. In fact, many experienced Java developers don’t understand when the use of volatile is necessary.
Java Thread Programming 1.7 - Concurrent Access to
时间: 2024-11-02 08:42:15
Java Thread Programming 1.7 - Concurrent Access to的相关文章
Java Thread Programming 1.7 - Concurrent Access to Objects and Variables
access|object 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 ap
Java Thread Programming 1.8.2 - Inter-thread Communication
Missed Notification A missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. In a multithreaded environment like Java, you don't have much control over which thread runs and for how lon
Java Thread Programming 1.8.3 - Inter-thread Communication
CubbyHole Example The class CubbyHole (see Listing 8.9) simulates a cubbyhole. A cubbyhole is a slot that can have only one item in it at a time. One thread puts an item into the slot and another thread takes it out. If a thread tries to put an item
Java Thread Programming 1.8.4 - Inter-thread Communication
Streaming Data Between Threads Using Pipes The java.io package provides many classes for writing and reading data to and from streams. Most of the time, the data is written to or read from a file or network connection. Instead of streaming data to a
Java Thread Programming 1.8.2 - Inter-thread Commu
Missed NotificationA missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. In a multithreaded environment like Java, you don't have much control over which thread runs and for how long.
Java Thread Programming 1.8.3 - Inter-thread Commu
CubbyHole ExampleThe class CubbyHole (see Listing 8.9) simulates a cubbyhole. A cubbyhole is a slot that can have only one item in it at a time. One thread puts an item into the slot and another thread takes it out. If a thread tries to put an item i
Java Thread Programming 1.8.1 - Inter-thread Communication
The Need for Inter-thread Signaling Through synchronization, one thread can safely change values that another thread will read. How does the second thread know that the values have changed? What if the second thread is waiting for the values to chang
Java Thread Programming 1.8.1 - Inter-thread Commu
The Need for Inter-thread SignalingThrough synchronization, one thread can safely change values that another thread will read. How does the second thread know that the values have changed? What if the second thread is waiting for the values to change
Java Thread Programming 1.8.4 - Inter-thread Commu
Streaming Data Between Threads Using PipesThe java.io package provides many classes for writing and reading data to and from streams. Most of the time, the data is written to or read from a file or network connection. Instead of streaming data to a f