how to restart a thread

前端 未结 5 1548
梦毁少年i
梦毁少年i 2021-01-21 22:58

I tried to write a file monitor which will check the file if a new line is appended,the monitor in fact is a thread which will read the line by a randomaccessfile all the time.

5条回答
  •  粉色の甜心
    2021-01-21 23:44

    Instead of explaining, I just coded up a skeleton example. I did not test it terribly well, but it may be of some use.

    In order to monitor a(nother) file, just create a new Monitor, passing it a ScheduledExecutorService. Starting and stopping monitoring is straightforward. You can (should) reuse the same executor for multiple monitors.

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    public interface Event
    {
    }
    
    public interface Listener
    {
        void handle(Event event);
    }
    
    public class Monitor
    {
        private static final int CHECK_EVERY_SECONDS = 10;
        private static final int RECHECK_AFTER_IF_NOT_EXISTS_SECONDS = 30;
    
        private File file;
        private ScheduledExecutorService executor;
        private boolean active;
        private List listeners;
    
        public Monitor(File file, ScheduledExecutorService executor)
        {
            super();
            this.file = file;
            this.executor = executor;
            listeners = new ArrayList();
        }
    
        public synchronized void start()
        {
            if (active)
            {
                return;
            }
            active = true;
            executor.execute(new Runnable()
            {
                public void run()
                {
                    synchronized (Monitor.this)
                    {
                        if (!active)
                        {
                            System.out.println("not active");
                            return;
                        }
                    }
                    if (!file.exists())
                    {
                        System.out.println("does not exist, rescheduled");
                        executor.schedule(this, RECHECK_AFTER_IF_NOT_EXISTS_SECONDS, TimeUnit.SECONDS);
                        return;
                    }
                    Event event = doStuff(file);
                    System.out.println("generated " + event);
                    updateListeners(event);
                    System.out.println("updated listeners and rescheduled");
                    executor.schedule(this, CHECK_EVERY_SECONDS, TimeUnit.SECONDS);
                }
            });
        }
    
        private Event doStuff(final File file)
        {
            return new Event()
            {
                public String toString()
                {
                    return "event for " + file;
                }
            };
        }
    
        public synchronized void stop()
        {
            active = false;
        }
    
        public void addListener(Listener listener)
        {
            synchronized (listeners)
            {
                listeners.add(listener);
            }
        }
    
        public void removeListener(Listener listener)
        {
            synchronized (listeners)
            {
                listeners.remove(listener);
            }
        }
    
        private void updateListeners(Event event)
        {
            synchronized (listeners)
            {
                for (Listener listener : listeners)
                {
                    listener.handle(event);
                }
            }
        }
    
        public static void main(String[] args) throws IOException
        {
            ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
            File file = new File("test.png");
            Monitor monitor = new Monitor(file, executor);
            monitor.addListener(new Listener()
            {
                public void handle(Event event)
                {
                    System.out.println("handling " + event);
                }
            });
            monitor.start();
            System.out.println("started...");
            System.in.read();       
            monitor.stop();
            System.out.println("done");
            executor.shutdown();
        }
    
    }
    

提交回复
热议问题