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

📄 timer.java

📁 network management, thread implement
💻 JAVA
字号:
// Developed by Kinva Network Inc. 2000


// Source File Name:   Timer.java

package com.kinva.util.thread;


// Referenced classes of package com.kinva.util.thread:
//            Timeout

public class Timer extends Thread
{
    private class Entry
    {

        void tick()
        {
            timeout.timeout(param);
        }

        Entry next;
        boolean periodic;
        Timeout timeout;
        Object param;
        long nextTime;

        Entry(Timeout timeout1, boolean flag, Object obj)
        {
            timeout = timeout1;
            periodic = flag;
            param = obj;
        }
    }


    public Timer(String s)
    {
        super(s);
        start();
    }

    public static Timer getTimer()
    {
        if(defaultTimer == null)
            defaultTimer = new Timer("CMDefaultTimer");
        return defaultTimer;
    }

    public void run()
    {
        while(!stopThread) 
        {
            Entry entry = getEntry();
            if(entry != null)
            {
                runTimeout(entry);
                if(entry.periodic)
                    add(entry);
            }
        }
    }

    private synchronized Entry getEntry()
    {
        while(!stopThread) 
        {
            try
            {
                if(head == null)
                    wait();
            }
            catch(InterruptedException interruptedexception)
            {
                interruptedexception.printStackTrace();
                continue;
            }
            if(stopThread)
                return null;
            long l = System.currentTimeMillis();
            long l1 = head.nextTime - l;
            if(l1 <= 0L)
            {
                Entry entry = head;
                head = head.next;
                currentEntry = entry;
                entry.next = null;
                return entry;
            }
            try
            {
                if(l1 > 0x5265c00L)
                    l1 = 0x5265c00L;
                wait(l1);
            }
            catch(InterruptedException interruptedexception1)
            {
                interruptedexception1.printStackTrace();
            }
        }
        return null;
    }

    private void runTimeout(Entry entry)
    {
        try
        {
            entry.tick();
            return;
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        entry.periodic = false;
    }

    private synchronized void add(Entry entry)
    {
        if(currentEntry == entry)
            currentEntry = null;
        long l = entry.timeout.getTimeoutPeriod();
        if(l < 0L)
            return;
        entry.nextTime = System.currentTimeMillis() + l;
        if(head == null)
        {
            head = entry;
            entry.next = null;
            notify();
            return;
        }
        Entry entry1 = head;
        Entry entry2 = null;
        for(; entry1 != null; entry1 = entry1.next)
        {
            if(entry.nextTime < entry1.nextTime)
                break;
            entry2 = entry1;
        }

        entry.next = entry1;
        if(entry2 == null)
        {
            head = entry;
            notify();
            return;
        } else
        {
            entry2.next = entry;
            return;
        }
    }

    public synchronized void remove(Timeout timeout)
    {
        if(head == null)
            return;
        Entry entry = head;
        Entry entry1 = null;
        for(; entry != null; entry = entry.next)
        {
            if(entry.timeout.equals(timeout))
            {
                if(entry1 == null)
                    head = entry.next;
                else
                    entry1.next = entry.next;
                break;
            }
            entry1 = entry;
        }

        if(currentEntry != null && currentEntry.timeout.equals(timeout))
            currentEntry.periodic = false;
    }

    public void startTimer(Timeout timeout)
    {
        startTimer(timeout, false, null);
    }

    public void startTimer(Timeout timeout, boolean flag)
    {
        startTimer(timeout, flag, null);
    }

    public void startTimer(Timeout timeout, Object obj)
    {
        startTimer(timeout, false, obj);
    }

    public void startTimer(Timeout timeout, boolean flag, Object obj)
    {
        add(new Entry(timeout, flag, obj));
    }

    public synchronized void shutdown()
    {
        stopThread = true;
        notifyAll();
    }

    private static final long MILLI_SECONDS_IN_DAY = 0x5265c00L;
    private static final long ETERNITY = 0x7fffffffffffffffL;
    private Entry head;
    private Entry currentEntry;
    private boolean stopThread;
    private static Timer defaultTimer;
}

⌨️ 快捷键说明

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