📄 mailboximpl.java
字号:
/* * @<#>MailBoxImpl.java version 0.0.1, 1/1/2000 * * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION. * * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS. * * Copyright (c) 2000 Cheng-Zhong Xu. All Rights Reserved. */package naplet.message;import java.rmi.RemoteException;import java.util.*;import java.io.*;import naplet.*;/** * The <code>MailBoxImpl</code> class implements <code>MailBox</code> * interface. Each long lived naplet is assigned to a mailbox once it * arrives at a server. * * @see MailBox * @version 0.0.1 * @author C. Xu */public class MailBoxImpl implements MailBox { private boolean newMailFlag = false; // New Mail private Vector mailbox; int mboxSize; /** * Constructs a mailbox * @param size initial size of the mailbox. By default, it is 10 */ public MailBoxImpl(int size) { if (size>0) { mailbox = new Vector(size); mboxSize = size; } else { mailbox = new Vector( DefaultMailBoxSize ); mboxSize = DefaultMailBoxSize; } } /** * Constructs a mailbox * By default, the initial mailbox size is 10. */ public MailBoxImpl() { mboxSize = DefaultMailBoxSize; mailbox = new Vector( DefaultMailBoxSize ); } /** * Insert a message into a mailbox. */ public synchronized void putMessage( Message msg ) throws InterruptedException { while ( mailbox.size() == mboxSize ) wait(); mailbox.addElement( msg ); newMailFlag = true; notify(); } /** * Fetch and remove the first mail from mailbox */ public synchronized Message getMessage() throws InterruptedException { while ( mailbox.isEmpty() ) wait(); Message msg = (Message) mailbox.firstElement(); mailbox.removeElement( msg ); notify(); newMailFlag = false; return msg; } /** * Try to get a mail for mailbox */ public synchronized Message tryGetMessage() { Message msg = null; if ( ! mailbox.isEmpty() ) { msg = (Message) mailbox.firstElement(); mailbox.removeElement( msg ); notify(); newMailFlag = false; } return msg; } /** * Try to get a mail from a specific sender */ public synchronized Message tryGetMessage(NapletID nid) { Message msg = null; boolean going = true; if ( going && !mailbox.isEmpty() ) { Enumeration enum = mailbox.elements(); while ( !enum.hasMoreElements() ) { Message m = (Message) enum.nextElement(); NapletID sid = m.getSender(); if ( nid.equals( sid ) ) { msg = m; mailbox.removeElement( msg ); notify(); going = false; } } newMailFlag = false; } return msg; } public synchronized void clearMailBox() { if ( !mailbox.isEmpty() ) { mailbox.removeAllElements(); newMailFlag = false; } } public synchronized boolean checkMailBox() { boolean flag = newMailFlag; newMailFlag = false; return flag; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -