栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何重新启动线程

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何重新启动线程

我没有解释,只是编写了一个框架示例。我没有对它进行很好的测试,但这可能有一定用处。

为了监视(另一个)文件,只需创建一个新的Monitor,并向其传递ScheduledExecutorService。启动和停止监视非常简单。您可以(应该)将同一个执行程序重用于多个监视器。

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<Listener> listeners;    public Monitor(File file, ScheduledExecutorService executor)    {        super();        this.file = file;        this.executor = executor;        listeners = new ArrayList<Listener>();    }    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();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号