﻿<?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-Sunny</title><link>http://www.blogjava.net/yangxp/</link><description>享受编程带来的快乐……</description><language>zh-cn</language><lastBuildDate>Wed, 29 Apr 2026 03:25:10 GMT</lastBuildDate><pubDate>Wed, 29 Apr 2026 03:25:10 GMT</pubDate><ttl>60</ttl><item><title>有条件的同步方法</title><link>http://www.blogjava.net/yangxp/archive/2006/10/09/74075.html</link><dc:creator>Xueping Yang</dc:creator><author>Xueping Yang</author><pubDate>Mon, 09 Oct 2006 06:27:00 GMT</pubDate><guid>http://www.blogjava.net/yangxp/archive/2006/10/09/74075.html</guid><wfw:comment>http://www.blogjava.net/yangxp/comments/74075.html</wfw:comment><comments>http://www.blogjava.net/yangxp/archive/2006/10/09/74075.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/yangxp/comments/commentRss/74075.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yangxp/services/trackbacks/74075.html</trackback:ping><description><![CDATA[    在做多线程程序时我们可能要对某段代码的同步是有条件的，只有对满足同一条件的请求才排队访问，对与不满足这样条件的请求可以并发访问，在处理这样的需求的时候我们可以容易的想到，对于这个特定的条件做一个抽象，让它作为我们同步锁。下面的代码可以说明这个处理过程：<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/dot.gif" /><img src="http://www.blogjava.net/images/dot.gif" /><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 定义锁</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Map locks </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap();<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Object getLock(Object key) {<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">synchronized</span><span style="color: rgb(0, 0, 0);"> (locks) {<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">locks.containsKey(key)) {<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">            locks.put(key, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Object());<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">        }<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> locks.get(key);<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/dot.gif" /><img src="http://www.blogjava.net/images/dot.gif" /><br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 需要同步的代码</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 255);">synchronized</span><span style="color: rgb(0, 0, 0);"> (getLock(id)) {<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> TODO</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/images/dot.gif" /><img src="http://www.blogjava.net/images/dot.gif" /><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"></span></div><br /><img src ="http://www.blogjava.net/yangxp/aggbug/74075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yangxp/" target="_blank">Xueping Yang</a> 2006-10-09 14:27 <a href="http://www.blogjava.net/yangxp/archive/2006/10/09/74075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>