??xml version="1.0" encoding="utf-8" standalone="yes"?>
private void testDel() {
上面的代码运行没有问题,但如果你?#8220;Ҏ一”替代“Ҏ?#8221;Q则会出现java.util.ConcurrentModificationException?br />
Q用for-each遍历也会ZcM问题Q?br />
具体原因是可以看一?span style="color: #000000;">先看看List中的removeҎ源码Q?br />
接着看。删除后得到下一个元素的代码Qit.next(): it为AbstractList的内部类Iterator的一个实例?br />
最后看Iterator的remove()Ҏ的源代码Q?br />
q下明白了Qlist的removeҎ只修改了modCount|而iterator的remove能同步modCount和expectedModCount.
]]>
German 1031 1031
French 1036 1036
Japanese 1041 1041
Danish 1030 1030
Spanish 3082 3082
Italian 1040 1040
Dutch 1043 1043
Norwegian 2068 2068
Portuguese 2070 2070
Finnish 1035 1035
Swedish 1053 1053
Czech 1029 1029
Hungarian 1038 1038
Polish 1045 1045
Romanian 1048 1048
Croatian 1050 1050
Slovak 1051 1051
Slovene 1060 1060
Greek 1032 1032
Bulgarian 1026 1026
Russian 1049 1049
Turkish 1055 1055
British English 2057 1033
Estonian 1061 1061
Latvian 1062 1062
Lithuanian 1063 1063
Brazilian 1046 1046
Traditional Chinese 1028 1028
Korean 1042 1042
Simplified Chinese 2052 2052
Arabic 1025 1025
Thai 1054 1054
volatile关键字有什么用Q?br /> 恐怕比较一下volatile和synchronized的不同是最Ҏ解释清楚的。volatile是变量修饰符Q而synchronized则作用于一D代码或ҎQ看如下三句get代码Q?/p>
geti1()得到存储在当前线E中i1的数倹{多个线E有多个i1变量拯Q而且q些i1之间可以互不相同。换句话_另一个线E可能已l改
变了它线E内的i1|而这个值可以和当前U程中的i1g相同。事实上QJava有个思想?#8220;?#8221;内存区域Q这里存放了变量目前?#8220;准确?#8221;。每个线E?
可以有它自己的变量拷贝,而这个变量拷贝值可以和“?#8221;内存区域里存攄不同。因此实际上存在一U可能:“?#8221;内存区域里的i1值是1Q线E?里的i1?
?Q线E?里的i1值是3——这在线E?和线E?都改变了它们各自的i1|而且q个改变q没来得及传递给“?#8221;内存区域或其他线E时׃发生?br />
而geti2()得到的是“?#8221;内存区域的i2数倹{用volatile修饰后的变量不允许有不同?#8220;?#8221;内存区域的变量拷贝。换句话_一个变量经
volatile修饰后在所有线E中必须是同步的QQ何线E中改变了它的|所有其他线E立卌取到了相同的倹{理所当然的,volatile修饰的变?
存取时比一般变量消耗的资源要多一点,因ؓU程有它自己的变量拷贝更为高效?br />
既然volatile关键字已l实CU程间数据同步,又要synchronizedq什么呢Q呵呵,它们之间有两点不同。首
先,synchronized获得q攄视器——如果两个线E用了同一个对象锁Q监视器能强制保证代码块同时只被一个线E所执行——这是众所周知的事
实。但是,synchronized也同步内存:事实上,synchronized?#8220;?#8221;内存区域同步整个U程的内存。因此,执行geti3()Ҏ?
了如下几步:
1. U程h获得监视this对象的对象锁Q假设未被锁Q否则线E等待直到锁释放Q?br />
2. U程内存的数据被消除Q从“?#8221;内存区域中读入(Java虚拟优化此步。。。[后面的不知道怎么表达,汗]Q?br />
3. 代码块被执行
4. 对于变量的Q何改变现在可以安全地写到“?#8221;内存区域中(不过geti3()Ҏ不会改变变量|
5. U程释放监视this对象的对象锁
因此volatile只是在线E内存和“?#8221;内存间同步某个变量的|而synchronized通过锁定和解锁某个监视器同步所有变量的倹{显然synchronized要比volatile消耗更多资源?/p>
附英文原文:
What does volatile do?
This is probably best explained by comparing the effects that volatile and synchronized have on a method. volatile is a field modifier, while synchronized modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords:
geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it’s thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a “main” memory, and this is the memory that holds the current “correct” value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the “main” memory. So in fact, it is possible for the “main” memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to “main” memory or other threads.
On the other hand, geti2() effectively accesses the value of i2 from “main” memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in “main” memory. Effectively, a variable declared volatile must have it’s data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile variables have a higher access and update overhead than “plain” variables, since the reason threads can have their own copy of data is for better efficiency.
Well if volatile already synchronizes data across threads, what is synchronized for? Well there are two differences. Firstly synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That’s the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with “main” memory. So executing geti3() does the following:
1. The thread acquires the lock on the monitor for object this
(assuming the monitor is unlocked, otherwise the thread waits until the
monitor is unlocked).
2. The thread memory flushes all its variables, i.e. it has all of its
variables effectively read from “main” memory (JVMs can use dirty sets
to optimize this so that only “dirty” variables are flushed, but
conceptually this is the same. See section 17.9 of the Java language
specification).
3. The code block is executed (in this case setting the return value to
the current value of i3, which may have just been reset from “main”
memory).
4. (Any changes to variables would normally now be written out to “main” memory, but for geti3() we have no changes.)
5. The thread releases the lock on the monitor for object this.
So where volatile only synchronizes the value of one variable between thread memory and “main” memory, synchronized synchronizes the value of all variables between thread memory and “main” memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.
摘自Qhttp://bianbian.org/technology/java/88.html
另一个著名的例子是Netscape Navigator 早期版本中的取决于时间的随机数字生成器,它泄露了动态生成的用于加密q用SSL的会话中数据的密钥?br />
About Random encrypted 加密的随机数
随机数生成是许多加密操作不可分割的组成部分。例如,加密密钥需要尽可能地随机,以便使它们很难被复制。加密随机数生成器必ȝ成在计算上无法进行推(低于 p < .05 的概率)的输出;卻IM推算下一个输Z的方法不得具有比随机猜测更高的成功几率?br />
Z说明一q串的随机数字是加密安全的,必须使得用户不可能通过计算重新生成同样序列的随机数。遗憄是,q用伪随机数字,可以很容易地重新生成同样的序列。用户需要知道的知识是伪随机数生成器算法和U子倹{?br />
通过加密保护数据Z加密法和更为随机的U子值就是本文要提出的方?一个带加密功能的随机数产生器,可以应用于需要加密随机数的场?Z构成U子|需要用不同的值组合成一个系l范围内的种子倹{这些值包括调用的应用E序可以提供的位Q例如鼠标或键盘动作之间的用户反应时间、象q程ID和线EIDq样的系l和用户数据、系l时钟、系l计数器、自q盘集属和散列的用户环境块。接着使用SHA-1散列q个|输出用于创徏一个随机数据流Q用于更新系l种子|。这可以起作用,是因为散列值生成了看似随机的数据,只改变源文档Q种子|中的一个位QQ何两个输出的散列׃n它们50%的位Q尽两个输出只有一位之差。当Ӟ从理ZԌ有些q程q是周期性的。例如磁盘搜索时间看似随机的Q实际取决于易于定的因素,可以被推出来。ؓ了获取更好的随机数生成,可以采用g生成器,例如Intel的随机数生成器?br />
说明Q?br />
创徏加密安全的随机数需要更多的旉Q这意味着如果需要快速地在一个短旉内生成大量随机数Q例如百万的数据)是不适合的。在一个简单测试中Q用本文提到的RNG生成一百万个随机数的时间花费差不多是伪随机数生成器所用时间的八倍之多?br />
相关资源Qhttp://www.xfocus.net/articles/200209/451.html
W一步:
windowsQ?gt;preferences->java->Editor->Code Assist ?/p>
autoQactivetion中的Enable auto-activetion选项要勾选上
q里面有个时间的讑֮是ؓ提示代码讑֮的显C时间。你可以Ҏ自己的情况设定?/p>
W二步:
另注Q也可以直接恢复成默认设|?/p>
Exception Stack Trace:
java.net.SocketTimeoutException: Accept timed out
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:353)
at java.net.ServerSocket.implAccept(ServerSocket.java:448)
at java.net.ServerSocket.accept(ServerSocket.java:419)
查看build.xml文g无异常,让我好是奇怪,明明昨天q是正常的呀?br />
后来查看一下Ant Home,发现只有四项Q觉得有点不对劲Q他l一看,居然指到?home/fingki/eclipse/plugins/Axis2_service_Archiver_1.3.0/lib下,
重新指定一下Ant Home,q行antQ运行了?br />
后来研究才发玎ͼ原来之前刚装了Axis2的plugins,Axis2_service_Archiver_1.3.0Q这样Eclipse重启后优先把Ant Home指向那了Q导致ant不工作了?br />
看来Axis2_service_Archiver_1.3.0q个pluginsq是有些bug呀?br />
具体讄Ant Home如下Q在Eclipse中-QwindowQ>preferencesQ>AntQ>RuntimeQ>ClasspathQ点击Ant home按钮Q重新让其指向eclipse中的ant的plugins文g夹处Q?br />
一般都?Eclipse_home%/plugins/org.apache.ant_1.x.x?br />