﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-朝着自己微笑的小胖-随笔分类-读书笔记</title><link>http://www.blogjava.net/shijian/category/44818.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 19 Dec 2011 20:53:49 GMT</lastBuildDate><pubDate>Mon, 19 Dec 2011 20:53:49 GMT</pubDate><ttl>60</ttl><item><title>Java concurrency in practice  </title><link>http://www.blogjava.net/shijian/archive/2011/12/18/366677.html</link><dc:creator>石建同学</dc:creator><author>石建同学</author><pubDate>Sun, 18 Dec 2011 14:56:00 GMT</pubDate><guid>http://www.blogjava.net/shijian/archive/2011/12/18/366677.html</guid><wfw:comment>http://www.blogjava.net/shijian/comments/366677.html</wfw:comment><comments>http://www.blogjava.net/shijian/archive/2011/12/18/366677.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shijian/comments/commentRss/366677.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shijian/services/trackbacks/366677.html</trackback:ping><description><![CDATA[&nbsp;<br />12月18日 chapter 2 Thread Safe<br /><br />1.what is thread safe ?<br />多个线程并发访问时，无需额外的同步或协调，能保证执行的正确性，则此类为线程安全。（结合Java特性，每个thread都会有独立的stack空间，因为仅在存在状态的类，才会导致thread safe问题。无状态的类，总是线程安全的。）<br /><div><p>A class is thread-safe if it  behaves correctly when accessed from multiple threads, regardless of the  scheduling or interleaving of the execution of those threads by the runtime  environment, and with no additional synchronization or other coordination on the  part of the calling code.<br /><br />2.Atomicity<br />如：servlet通过count属性记录访问数，此类是线程不安全的。原因是：count++，并非原子操作；实际是3个步骤，读取count、加1、写回主存，其中任何阶段其它线程均可进行操作，若count++操作时原子时？是保证计数功能是线程安全的。<br />race condition : The possibility of incorrect results in the presence of unlucky timing is so important in concurrent programming that it has a name: a race condition.<br /></p><div>The most common type of race condition is check-then-act, where a potentially stale observation  is used to make a decision on what to do next. 最常见的race condition操作，'check-then-act' -&gt; '读取-操作'（lazy init），此时会导致使用一个stale状态去决定下一步的操作。（或count++操作，read-modify-write类型）<br /><div><p>Both <tt>LazyInitRace</tt> and  <tt>UnsafeCountingFactorizer</tt> contained a sequence of operations that needed  to be atomic, or indivisible, relative to other  operations on the same state. To avoid race conditions, there must be a way to  prevent other threads from using a variable while we're in the middle of  modifying it, so we can ensure that other threads can observe or modify the  state only before we start or after we finish, but not in the middle.<br />什么是原子性 ？一个操作，要么全部完成，要么没有发生，不会存在中间状态。<br /><br />3.Locking<br /></p><div>But if we want to add more state to our  servlet, can we just add more thread-safe state variables ? 必须保证相关状态操作的原子性。Java provides a built-in locking mechanism for enforcing atomicity: the  <tt>synchronized</tt> block.&nbsp;<br />Every Java object can implicitly act as a lock for purposes of synchronization。锁是属于实例的，类级别对应的实例是Class对象。且在任意时刻仅有一个线程持有此锁。某线程进去被synchronized保护的代码块，必须首先获取到此锁，否则wait直到获取此锁。</div><div>3.1&nbsp;Reentrancy<br /><div>But because intrinsic locks are reentrant, if a  thread tries to acquire a lock that it already  holds, the request succeeds.&nbsp;</div><div>Reentrancy is implemented by associating with each lock an acquisition count and  an owning thread. 实现依赖锁被获取的次数和拥有此锁的线程。<br />当每次获取到此锁时，acquisition&nbsp;count加1；当退出synchronized时，count减1；直到count为0时，释放此锁。</div>想象：若synchronized是非重入的，会有什么结果 ？<br /><br />4.Guarding state with lock</div>sync保证compound actions原子性；对共享变量的任何操作都需要显示的去sync，这是一个问题；<br />对象内建的lock与属性并没有固有的关系，内建lock方便去创建sync policy去保证thread safe；<br />一种简单的方法，对所有状态统一使用内建的lock，意味着在任何action code path都需要去sync，如：Vector；<br />For every invariant that involves more than one variable, all the variables involved in that invariant must be  guarded by the same lock.<br />滥用sync会导致性能的问题。<br /><br />5.Liveness and Performance<br />sync会导致性能问题。原则：sync块尽量small，sync操作不能分割太细。如何做，需要从safe、simple、performance考虑。<br /><div>Deciding how big or small to make <tt>synchronized</tt> blocks may require  tradeoffs among competing design forces, including safety (which must not be  compromised), simplicity, and performance.通常简单性和性能是相互牵制的，不要为了性能过早的牺牲简单性（是潜在的对safe的妥协）。<br />Avoid holding locks during lengthy computations or operations  at risk of not completing quickly such as network or console I/O.<br /></div></div></div><div></div></div><img src ="http://www.blogjava.net/shijian/aggbug/366677.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shijian/" target="_blank">石建同学</a> 2011-12-18 22:56 <a href="http://www.blogjava.net/shijian/archive/2011/12/18/366677.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>信息的表示和处理</title><link>http://www.blogjava.net/shijian/archive/2010/04/24/319285.html</link><dc:creator>石建同学</dc:creator><author>石建同学</author><pubDate>Sat, 24 Apr 2010 13:39:00 GMT</pubDate><guid>http://www.blogjava.net/shijian/archive/2010/04/24/319285.html</guid><wfw:comment>http://www.blogjava.net/shijian/comments/319285.html</wfw:comment><comments>http://www.blogjava.net/shijian/archive/2010/04/24/319285.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/shijian/comments/commentRss/319285.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/shijian/services/trackbacks/319285.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt; "><span style="font-family: 新宋体; ">
<p class="MsoNormal"><font  face="宋体">
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">计算机以二进制表示和存储信息。</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">计算机内部数据的存储，根据数据的特性分为：</span></p>
<p style="margin-left:39.0pt;text-indent:-18.0pt;
tab-stops:list 39.0pt"><span style="Times New Roman&quot;">1、&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">无符号整数</span></p>
<p style="margin-left:39.0pt;text-indent:-18.0pt;
tab-stops:list 39.0pt"><span style="Times New Roman&quot;">2、&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有符号整数（补码）</span></p>
<p style="margin-left:39.0pt;text-indent:-18.0pt;
tab-stops:list 39.0pt"><span style="Times New Roman&quot;">3、&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">浮点数</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">明确的关键点：</span></p>
<p style="margin-left:39.0pt;text-indent:-18.0pt;
tab-stops:list 39.0pt"><span style="Times New Roman&quot;">1、&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">计算机内部是通过<span style="color:#993300">有限的位</span>编码一个数字，因此存在&#8220;溢出&#8221;现象，因为表示的范围是有限的。</span></p>
<p style="margin-left:39.0pt;text-indent:-18.0pt;
tab-stops:list 39.0pt"><span style="Times New Roman&quot;">2、&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">大多数计算机使用</span>8<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">位的块，做为最小的可寻址的存储器单位。</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">、不同的数据类型，数据大小是不一样的。（准确的要依赖机器和编译器）。</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">、对象的地址使用字节序列中的最小的地址。（网络传输、跨平台时考虑字节序的问题）<br />
<br />
</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">一、无符号整数</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">基本的&#8220;二进制&#8221;表示。</span>&nbsp;<br />
<br />
</p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">二、有符号整数</span> --- <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">补码</span> <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">【<span style="color:#993300">同余</span>】</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">使用补码，减法也可按加法来处理（</span>ALU<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">不用去实现减法运算）。（两个用补码表示的数据相加时候，将符号位和其它位统一处理，如果最高位</span>(<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">符号位</span>)<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有进位，则进位被舍弃。）</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">最高有效位为符号位，</span>1<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">负数，</span>0<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">正数。(</span>Java<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">只支持有符号整数)<br />
<br />
</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">三、浮点数</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">划分为三个域：</span><span style="Times New Roman&quot;">1.</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">符号 &nbsp;</span><span style="Times New Roman&quot;">2.</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有效位（二进制小数） &nbsp;</span><span style="Times New Roman&quot;">3.&nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">指数位（</span>2<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的幂，对浮点数加权）</span></p>
<p style="margin-left:21.0pt"><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">单精度：</span>1 = 23 =8 <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">；</span>&nbsp;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">双精度</span> <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">：</span>1 = 52 = 11<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">特性：精度有限，浮点运算是不可结合和交换的。</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">四、&#8220;模&#8221;概念</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>&#8221;<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">是指一个计量系统的计数范围</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">如时钟等</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">计算机也可以看成一个计量机器</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">它也有一个计量范围</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">即都存在一个</span>&#8220;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>&#8221;.<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">例如：</span><span> <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">时钟的计量范围是</span>0<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">～</span>11,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>=12. </p>
<p>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">表示</span>n<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">位的计算机计量范围是</span>0<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">～</span>2(n)-1,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>=2<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">（</span>n<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">【注：</span>n<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">表示指数】</span><span> <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;</span><span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>&#8221;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">实质上是计量器产生</span>&#8220;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">溢出</span>&#8221;<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">的量</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">它的值在计量器上表示不出来</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">计量器上只能表示出模的余数</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">任何有模的计量器</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">均可<span style="color:#993300">化减法为加法运算</span></span>. <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">例如：</span> <span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">假设当前时针指向</span>10<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">点</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">而准确时间是</span>6<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">点</span>,<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">调整时间可有以下两种拨法：</span><span> <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;1</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">、倒拨</span>4<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">小时</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">即：</span><span>10-4=6 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;2</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">、顺拨</span>8<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">小时：</span><span>10+8=12+6=6
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">在以</span>12<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模的系统中</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">加</span>8<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">和减</span>4<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">效果是一样的</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">因此凡是减</span>4<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">运算</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">都可以用加</span>8<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">来代替</span><span>. <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">对</span>&#8220;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">模</span>&#8221;<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">而言</span>,8<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>4<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">互为补数</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">实际上以</span>12<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">模的系统中</span>,11<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>1,10<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>2,9<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>3,7<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>5,6<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>6<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">都有这个特性</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">共同的特点是两者相加等于模</span><span>.
<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">对于计算机</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">其概念和方法完全一样</span>.n<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">位计算机</span>,<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">设</span>n=8, <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">所能表示的最大数是</span>11111111,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">若再加</span>1<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">称为</span>100000000(9<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">位</span>),<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">但因只有</span>8<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">位</span>,<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">最高位</span>1<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">自然丢失</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">又回了</span>00000000,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">所以</span>8<span style="font-family:
宋体;Times New Roman&quot;;Times New Roman&quot;">位二进制系统的模为</span>2(8). <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">在这样的系统中减法问题也可以化成加法问题</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">只需把减数用相应的补数表示就可以了</span>.<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">把补数用到计算机对数的处理上</span>,<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就是补码。<br />
<br />
<br />
<br />
<br />
</span></p>
</font></p>
</span></span>
<img src ="http://www.blogjava.net/shijian/aggbug/319285.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/shijian/" target="_blank">石建同学</a> 2010-04-24 21:39 <a href="http://www.blogjava.net/shijian/archive/2010/04/24/319285.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>