Java Thread Programming 1.7 - Concurrent Acce…

2008-02-23 09:36:10来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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.
Volatile关键字,如果用来修饰成员变量,则强迫每个线程每次访问这个变量都要重新读取,每次改变变量值时都要尽快回写。这样,两个不同的线程在特定时间总能获得相同的变量值
Use volatile on member variables that can be accessed by two or more threads unless all the threads access the variables within synchronized blocks of code. If a member variable remains constant after construction (it is read-only), there is no need for it to be volatile.
如果一个成员变量是只读的,则没有必要设置为volatile
如果一个所有的线程访问这个变量都是在一个同步代码块中,则没有必要设置为volatile
The volatile modifier exists to request that the VM always access the shared copy of the variable. This is less efficient than allowing the VM to perform optimizations by keeping a private copy. You should use volatile only when it is necessary; overuse will unnecessarily slow the application’s execution.
Volatile要求虚拟机的每次对变量的请求都要回访,这对于每个线程保持一个私有拷贝是低效的,会降低访问速度,如非必要,不要使用
synchronized Method Modifier
The addition of the synchronized modifier to a method declaration ensures that only one thread is allowed inside the method at a time. This can be useful in keeping out other threads while the state of an object is temporarily inconsistent.
Synchronized修饰,用来修饰一个方法申明,可以保证同一时间只有一个线程能够调用此方法。
这种机制能够防止对象处于不连续状态时被其他线程访问
Two Threads Simultaneously in the Same Method of One Object
If two or more threads are simultaneously inside a method, each thread has its own copy of local variables.
两个或多个线程同时调用同一对象的相同方法,则每一个线程拥有局部变量的拷贝
One Thread at a Time
More than one thread can be inside a method, and each thread keeps a copy of its own local variables. However, there are times when application constraints require that only one thread be permitted inside a method at a time.
When a thread encounters a synchronized instance method, it blocks until it can get exclusive access to the object-level mutex lock. Mutex is short for mutual exclusion. A mutex lock can be held by only one thread at a time. Other threads waiting for the lock will block until it is released. When the lock is released, all the threads waiting for it compete for exclusive access. Only one will be successful, and the other threads will go back into a blocked state waiting for the lock to be released again.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java Thread Programming 1.8.1 - Inter-thread Communication

下一篇:Pointcut的学习(三)