JavaÏß³ÌÖ®Read Write Lock Pattern
Read-Write Lock PatternµÄ²ÎÓëÕߣº
1. Reader£¨¶ÁÈ¡Õߣ©²ÎÓëÕß
Reader²ÎÓëÕß»á¶ÔSharedResource²ÎÓëÕß½øÐÐread¡£
2. Writer£¨Ð´ÈëÕߣ©²ÎÓëÕß
Writer²ÎÓëÕß»á¶ÔSharedResource²ÎÓëÕß½øÐÐwrite¡£
3. SharedResource£¨¹²Ïí×ÊÔ´£©²ÎÓëÕß
SharedResource²ÎÓëÕß´ú±íReaderÓëWriterËù¹²ÏíµÄ×ÊÔ´¡£
SharedResource²ÎÓëÕß»áÌṩ²»»á¸Ä±äÄÚ²¿×´Ì¬µÄ²Ù×÷£¨read£©ºÍ»á¸Ä±äÄÚ²¿×´Ì¬µÄ²Ù×÷£¨write£©¡£
4. ReadWriteLock£¨¶ÁдÓÃËø¶¨£©²ÎÓëÕß
ReadWriteLock²ÎÓëÕßÌṩÁ˶ÔSharedResource²ÎÓëÕß½øÐÐread²Ù×÷Óë½øÐÐwrite²Ù×÷ʱÐèÒªµÄËø¶¨¡£
ΪÁËÍê³Éread²Ù×÷£¬ÌṩÁËreadLockÓëreadUnlock¡£
ΪÁËÍê³Éwrite²Ù×÷£¬ÌṩÁËwriteLockºÍwriteUnlock¡£
------------------------------
Read-Write Lock PatternÊʺ϶ÁÈ¡±ÈдÈë´ÎÊýƵ·±µÄÇé¿ö¡£
Read-Write Lock PatternÀîÓÀÀÖ¶ÁÈ¡²Ù×÷±Ë´ËÖ®¼ä²»»á³åÍ»µÄÌØÐÔ£¬Ê¹µÃ¶à¸öReader²ÎÓëÕß¿ÉÒÔͬʱ½øÐÐread£¬ÒÔÌáÉý³ÌÐòµÄÐÔÄÜ¡£
²»¹ýµ±Ð´Èë²Ù×÷ƵÂʽϸßʱ£¬Writer²ÎÓëÕ߻ᾳ£×èÀ¹Reader²ÎÓëÕßµÄread²Ù×÷µÄ½øÐУ¬ÕâÑùÒ²¾ÍÎÞ·¨Õ¹ÏÖRead-Write Lock PatternµÄÓŵãÁË¡£
------------------------------
Ò»¸ö ReadWriteLock²ÎÓëÕßµÄʵÏÖ£º
public final class ReadWriteLock{
private int readingReaders = 0; // ʵ¼ÊÕýÔÚ¶ÁÈ¡µÄÏß³ÌÊý
private int waitingWriters = 0; // ÕýÔڵȴýдÈëµÄÏß³ÌÊý
private int writingWriters = 0; // ʵ¼ÊÕýÔÚдÈëµÄÏß³ÌÊý
private boolean preferWriter = true; // ÊÇ·ñдÈëÓÅÏÈ
public synchronized void readLock() throws InterruptedException{
while(writingWriters >0 ||(preferWriter && waitingWriters >0)){
wait();
}
readingReaders++;
}
public synchronized void readUnlock() throws InterruptedException{
readingReaders--;
notifyAll();
}
public synchronized void writeLock() throws InterruptedException{
waitingWriters++;
try{
while(readingReaders>0||writingWriters>0){
wait();
}
}finally{
waitingWriters--;
}
writingWriters++;
}
public synchronized void writeUnlock() throws InterruptedException{
writingWriters--;
noti
Ïà¹ØÎĵµ£º
JAVA¹Ø¼ü×ÖÏê½â
1 finalÊý¾Ý£º
1>¶ÔÓÚ»ù±¾ÀàÐÍÇ°¼ÓÒÔfinalÐÞÊΣ¬±íʾ±»ÐÞÊεıäÁ¿Îª³£Êý£¬²»¿ÉÒÔÐ޸ġ£Ò»¸ö¼ÈÊÇstaticÓÐÊÇfinalµÄ×ֶαíʾֻռ¾ÝÒ»¶Î²»ÄܸıäµÄ´æ´¢¿Õ¼ä¡£
2>finalÓÃÓÚ¶ÔÏóÓ¦ÓÃʱ£¬finalʹӦÓú㶨²»±ä¡£Ò»µ©ÒýÓñ»³õʼ»¯Ö¸ÏòÒ»¸ö¶ÔÏ󣬾ÍÎÞ·¨ÔÙ°ÑËüÖ¸ÏòÁíÒ»¸ö¶ÔÏó¡£
& ......
ÔÚʵÏÖsingletonģʽʱ£¬ÎÒÃÇÓÐÒÔϼ¸ÖÖ·½·¨¡£ 1. public static final ×ֶμÓÉÏprivate µÄ¹¹Ô캯Êý¡£ public class Singleton{
public static final Singleton INSTANCE = new Singleton();
......
Java¿ì½Ý¼üÖÆ×÷
ÓùýVBµÄÖªµÀ°´ÏÂF5ÊÇÔËÐд°Ì壬ÓùýPhotoshopµÄÖªµÀ°´ÏÂTabÊÇÒþ²ØËùÓÐÃæ°å
£¬ÄãµÄJava³ÌÐòÒ²ÏëÓÐÕâ¸ö¹¦ÄÜô£¿ÄÇôϣÍûÕâƪÎÄÕ¶ÔÄúÓÐËù°ïÖú¡£
×Ӳ˵¥ÀàJMenuItemÓÐÒ»¸ö·½·¨²»ÖªµÀÄã×¢Òâ¹ýûÓÐ:setAccelerator¡£
´ò¿ª°ïÖúÎļþ¶ÔÕâ¸ö·½·¨µÄÃèÊöÊÇ£º ÉèÖÃ×éºÏ¼ü£¬ËüÄÜÖ±½Óµ÷Óò˵¥ÏîµÄ²Ù×÷
ÕìÌýÆ÷¶ø²»±ØÏ ......
Ìù×ÓÖв¿·ÖÔ´´úÂëÕª×ÔcsdnµÄһƪÌû×Ó£¬ ²Î¿¼ÁËËûÃǵÄÀí½â£¬ËµËµ×Ô¼ºµÄһЩ¼û½â£º
//Ô´´úÂ룺
class A {
public String show(D obj){
return ("A and D");
......
Õâ¸öËã·¨¼òµ¥,¶øÇÒЧÂʸß,ÿ´Î¿ÉÒÔ²Ù×÷8¸ö×Ö½ÚµÄÊý¾Ý,¼ÓÃܽâÃܵÄKEYΪ16×Ö½Ú,¼´°üº¬4¸öintÊý¾ÝµÄintÐÍÊý×é,¼ÓÃÜÂÖÊýӦΪ8µÄ±¶Êý,Ò»°ã±È½Ï³£ÓõÄÂÖÊýΪ64,32,16,ÍƼöÓÃ64ÂÖ.
Ô´´úÂëÈçÏÂ:
/** *//**
* TeaËã·¨
* ÿ´Î²Ù×÷¿ÉÒÔ´¦Àí8¸ö×Ö½ÚÊý¾Ý
* KEYΪ16×Ö½Ú,ӦΪ°üº¬4¸öintÐÍÊýµÄint[]£¬Ò»¸öintΪ4¸ö×Ö½Ú
* ¼ÓÃܽâÃÜÂÖÊ ......