`
duguyidao
  • 浏览: 138252 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

example:out of order write

阅读更多
17.11 Example: Out-of-Order Writes
This example is similar to that in the preceding section, except that one method assigns to both variables and the other method reads both variables. Consider a class that has class variables a and b and methods to and fro:
class Simple {
int a = 1, b = 2;
void to() {
a = 3;
b = 4;
}
void fro() {
System.out.println("a= " + a + ", b=" + b);
}
}
Now suppose that two threads are created, and that one thread calls to while the other thread calls fro. What is the required set of actions and what are the ordering constraints?

Let us consider the thread that calls to. According to the rules, this thread must perform an assign of a followed by an assign of b. That is the bare minimum required to execute a call to the method to. Because there is no synchronization, it is at the option of the implementation whether or not to store the assigned values back to main memory! Therefore the thread that calls fro may obtain either 1 or 3 for the value of a, and independently may obtain either 2 or 4 for the value of b.

Now suppose that to is synchronized but fro is not:
class SynchSimple {
int a = 1, b = 2;
synchronized void to() {
a = 3;
b = 4;
}
void fro() {
System.out.println("a= " + a + ", b=" + b);
}
}
In this case the method to will be forced to store the assigned values back to main memory before the unlock action at the end of the method. The method fro must, of course, use a and b (in that order) and so must load values for a and b from main memory.

The total set of actions may be pictured as follows:



Here an arrow from action A to action B indicates that A must precede B.

In what order may the actions by the main memory occur? Note that the rules do not require that write a occur before write b; neither do they require that read a occur before read b. Also, even though method to is synchronized, method fro is not synchronized, so there is nothing to prevent the read actions from occurring between the lock and unlock actions. (The point is that declaring one method synchronized does not of itself make that method behave as if it were atomic.)

As a result, the method fro could still obtain either 1 or 3 for the value of a, and independently could obtain either 2 or 4 for the value of b. In particular, fro might observe the value 1 for a and 4 for b. Thus, even though to does an assign to a and then an assign to b, the write actions to main memory may be observed by another thread to occur as if in the opposite order.

Finally, suppose that to and fro are both synchronized:
class SynchSynchSimple {
int a = 1, b = 2;
synchronized void to() {
a = 3;
b = 4;
}
synchronized void fro() {
System.out.println("a= " + a + ", b=" + b);
}
}
In this case, the actions of method fro cannot be interleaved with the actions of method to, and so fro will print either "a=1, b=2" or "a=3, b=4".


Site Feedback
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement  
  • 大小: 7.9 KB
分享到:
评论

相关推荐

    英语必修一二基础词汇复习大全.pdf

    出来:come out of 12. 太...而不能...:too...to... 13. 分发,发出(气味等):give off 14. 被困在废墟中:be trapped under ruins 15. 代替,而不是:instead of 16. 纪念---,向---表示敬意:in memory of 17. ...

    matlabmatrix.rar_4 3 2 1_Fibonacci

    1) Write a function reverse(A) which takes a matrix A of arbitrary dimensions as input and returns a matrix B consisting of the columns of A in reverse order. Thus for example, if A = 1 2 3 then B = ...

    Test-.Driven.Python.Development.1783987928

    In order to get the best out of this book, you should have development experience with Python. In Detail This book starts with a look at the test-driven development process, and how it is different ...

    drupal 6.12

    name of each file for CHANGELOG.txt in the example. For more information on setting file permissions, see "Modifying Linux, Unix, and Mac file permissions" (http://drupal.org/node/202483) or ...

    Java邮件开发Fundamentals of the JavaMail API

    take advantage of protocols for which Sun does not provide out-of-the-box support. You'll find support for NNTP (Network News Transport Protocol) [newsgroups], S/MIME (Secure Multipurpose Internet ...

    华南理工大学计算机全英班算法设计实验

    4)Write down the report in which there should be the execution results of the program. 5. Example code with C++ ………. void myquicksort(int* A, int l,int r) { if(l>=r) return ; int i=l,j=r; int...

    闭包搜索算法java编程

    find out how many pairs of events are concurrent. Input The input will include first an integer, nc, specifying the number of computations in the test case. For each of these nc computations there ...

    Universal-USB-Installer

    For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get ...

    BobBuilder_app

    Theoretically a b+tree is O(N log k N) or log base k of N, now for the typical values of k which are above 200 for example the b+tree should outperform any binary tree because it will use less ...

    应用Dephi 开发佳能照相机API

    Out: outCount - Number of elements in this list. Returns: Returns EDS_ERR_OK if successful. In other cases, see EDSDKError.pas. --------------------------------------------------------------------...

    Hibernate Reference Documentation3.1

    11.2.4. Transaction timeout 11.3. Optimistic concurrency control 11.3.1. Application version checking 11.3.2. Extended session and automatic versioning 11.3.3. Detached objects and automatic ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...

    squashfs1.3r3.tar.gz

    write a new filesystem irrespective of whether an existing filesystem is present. The -e and -ef options allow files/directories to be specified which are excluded from the output filesystem. The -e ...

    cJSON-master.zip

    If you are packaging cJSON for a distribution of Linux, you would probably take these steps for example: ``` mkdir build cd build cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=Off -DCMAKE_...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    This chapter starts with an example of introspecting an Excel workbook (i.e., presenting how many worksheets the workbook contains, the names of the worksheets, and the number of rows and columns in ...

    微软内部资料-SQL性能优化3

    In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...

    BigFish 文档

    - 2.2 Example **概述**部分提供了 BigFish 框架的基本介绍,包括框架的目的、特点和应用场景。此外,通过示例来展示如何使用 BigFish 构建电子商务网站。这部分内容为读者提供了对框架的整体认识,并通过具体示例...

    Sybex - Mastering Visual Basic .NET (VBL).pdf

    most out of Visual Basic. It covers the topics I feel are of use to most VB programmers, and it does so in depth. Visual Basic .NET is an extremely rich programming environment, and I’ve had to ...

    折半查找 binarySearch

    A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. ...

Global site tag (gtag.js) - Google Analytics