1. 程式人生 > >MINA SOCKET SERVER學習筆記(二)Writing Protocol Codec

MINA SOCKET SERVER學習筆記(二)Writing Protocol Codec

  • MINA ensures that there will never be more than one thread simultaneously executing the decode() function for the same IoSession, but it does not guarantee that it will always be the same thread. Suppose the first piece of data is handled by thread-1 who decides it cannot yet decode, when the next piece of data arrives, it could be handled by another thread. To avoid visibility problems, you must properly synchronize access to this decoder state (IoSession attributes are stored in a ConcurrentHashMap
    , so they are automatically visible to other threads).
    MINA確保對每一個IoSession同一時刻只有一個執行緒在執行decode() 函式——不允許多執行緒併發地執行解碼函式,但它並不能保證每次解碼過程都是同一執行緒在執行(兩次解碼用的可能是不同的執行緒)。假設第一塊資料被執行緒1管理,這時還沒接收到足夠的資料以供解碼,當接收到第二塊資料時,被另一個執行緒管理,為了避免視覺化方面的問題,你就必須適當地同步對解碼器狀態的訪問(IoSession的屬性儲存在支援併發訪問的ConcurrentHashMap中,因此它們對其他執行緒是自動可見的)。