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

📄 clock_timer.java

📁 用java写的时钟小程序
💻 JAVA
字号:
/*
 * Clock_Timer.java
 *
 * Created on 2007年10月31日, 下午3:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package clock;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Vector;

/**
 *
 * @author xiaohai
 */
public class Clock_Timer extends UnicastRemoteObject
        implements Clock_Subject{
    
    /** Creates a new instance of Clock_Timer */
    public Clock_Timer() throws RemoteException {
        super();
        Vector_Observer=new Vector(50);
        Vector_Observer_ip=new Vector(50);
        mode=new int[50];
        count=new int[50];
    }
    
    public void Attach(String ip, int time) throws RemoteException {
        Clock_Observer co=null;
        try {
            co=(Clock_Observer)Naming.lookup(ip);
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (NotBoundException ex) {
            ex.printStackTrace();
        }
        
        Vector_Observer.add(co);
        Vector_Observer_ip.add(ip);
        mode[Vector_Observer.size()-1]=time;
        count[Vector_Observer.size()-1]=time;
        System.out.println(ip+"update time is "+time+"\n");
        System.out.println("it is the "+Vector_Observer.size()+" observer\n");
        
    }
    
    public void Detach(String ip) throws RemoteException{
        Clock_Observer co=null;
        
        try {
            co=(Clock_Observer)Naming.lookup(ip);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (NotBoundException ex) {
            ex.printStackTrace();
        }
        
        int l=Vector_Observer.size();
        int j=Vector_Observer_ip.indexOf(ip);
        Vector_Observer_ip.remove(j);
        Vector_Observer.remove(j);
        
        for(int i=j;i<l-1;i++) {
            mode[i]=mode[i+1];
            count[i]=count[i+1];
        }
    }
    
    public void Notify() throws RemoteException {
        
        System.out.println("Notify the observer\n");
        
        int Length=Vector_Observer.size();
        if(Length>0){
            for(int i=0;i<Length;i++) {
                int j=count[i]-1;
                System.out.println("the "+i+" update time is "+count[i]+"\n");
                if(j<=0) {
                    System.out.println("begin update!\n");
                    System.out.println("update the "+i+"observer\n");
                    Clock_Observer co=(Clock_Observer)Vector_Observer.get(i);
                    co.Update();
                    System.out.println("the "+i+" observer has been updated!\n");
                    count[i]=mode[i];
                } else
                    count[i]=j;
            }
        } else System.out.println("no observer connect\n");
    }
    
    public int GetHour() throws RemoteException {
        c1=new Date();
        return c1.getHours();
    }
    
    public int GetMinute() throws RemoteException {
        return c1.getMinutes();
    }
    
    public int GetSecond() throws RemoteException {
        return c1.getSeconds();
    }
    
    public void Tick() throws RemoteException {
        th = new InnerThread();
        th.setPriority(Thread.MIN_PRIORITY);
        th.start();
        
    }
    private class InnerThread extends Thread {
        public void run(){
            while(true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                try {
                    Notify();
                } catch (RemoteException ex) {
                    ex.printStackTrace();
                }
                
            }
        }
    }
    
 /*
  私有成员
  */
    private Vector Vector_Observer;
    private Vector Vector_Observer_ip;
    private Date c1 = null;
    private int hour;
    private int minute;
    private int second;
    private int mode[];
    private int count[];
    
    private InnerThread th = null;
}

⌨️ 快捷键说明

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