⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showtimeservant.java

📁 模拟实现分布式RMI调用时间显示,在两台电脑上实现,运用分布式RMI调用和回调,在Windows命令行下操作.
💻 JAVA
字号:
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
import java.text.SimpleDateFormat;

public class ShowTimeServant extends UnicastRemoteObject implements ShowTimeInterface, Runnable {
	
	private Vector <CallBackInterface>clientlist;
	private HashMap hashlist;
	private String time;
	
	public ShowTimeServant() throws RemoteException {
		clientlist = new Vector();
		hashlist = new HashMap();
	}
	
	public void register(CallBackInterface client) throws RemoteException {
		System.out.println("Add a client");
		clientlist.add(client);
		hashlist.put(client, new Integer(client.getInterval()));
		System.out.println("current number of client is :" + clientlist.size());
	}
	
	public void deregister(CallBackInterface client) throws RemoteException {
		System.out.println("Remove resigter: " + client);
		clientlist.remove(client);
	}
	
	public void run() {
		try{
			for(;;) {
				System.out.println("Start the hole process one more time.");
				System.out.println();
				Thread.sleep(1000);
				for (int i = 0; i < clientlist.size(); i++) {
					CallBackInterface client = clientlist.get(i);
					System.out.println("Process client :" + i);
					try {
						int interval = client.getInterval();
						System.out.println("the client's interval is :" + interval);
						Integer timeleft = (Integer)hashlist.get(client);
						int count = timeleft.intValue();
						System.out.println("the count of client " + i + " is " + count);
						count--;
						if (count == 0) {
							count = interval;
							Date date = new Date();
							SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							time = format.format(date);
							client.updateTime(time);
						}
						hashlist.put(client, count);
					} catch (RemoteException e) {
						System.out.println("remove a client");
						clientlist.remove(client);
						hashlist.remove(client);
					}				
				}
			}
		} catch (InterruptedException ie) { }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -