how to restart a thread

假如想象 提交于 2019-12-20 03:08:05

问题


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.

This is the monitor core codes:

public class Monitor {
    public static Logger                                    log             = Logger.getLogger(Monitor.class);
    public static final Monitor             instance        = new Monitor();
    private static final ArrayList<Listener>    registers       = new ArrayList<Listener>();

    private Runnable                                        task            = new MonitorTask();
    private Thread                                          monitorThread   = new Thread(task);
    private boolean                                         beStart         = true;

    private static RandomAccessFile                         raf             = null;
    private File                                            monitoredFile   = null;
    private long                                            lastPos;

    public void register(File f, Listener listener) {
        this.monitoredFile = f;
        registers.add(listener);
        monitorThread.start();
    }

    public void replaceFile(File newFileToBeMonitored) {
        this.monitoredFile = newFileToBeMonitored;

        // here,how to restart the monitorThread?
    }

    private void setRandomFile() {
        if (!monitoredFile.exists()) {
            log.warn("File [" + monitoredFile.getAbsolutePath()
                    + "] not exist,will try again after 30 seconds");
            try {
                Thread.sleep(30 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setRandomFile();
            return;
        }
        try {
            if (raf != null) {
                raf.close();
                lastPos = 0;
            }
            raf = new RandomAccessFile(monitoredFile, "r");
            log.info("monitor file " + monitoredFile.getAbsolutePath());
        } catch (FileNotFoundException e) {
            // The file must exist now
        } catch (IOException e) {}
    }

    private void startRead() {
        beStart = true;
        String line;
        while (beStart) {
            try {
                raf.seek(lastPos);
                while ((line = raf.readLine()) != null) {
                    fireEvent(new FileEvent(monitoredFile.getAbsolutePath(),
                            line));
                }
                lastPos = raf.getFilePointer();
            } catch (IOException e1) {}
        }
    }

    private void stopRead() {
        this.beStart = false;
    }

    private void fireEvent(FileEvent event) {
        for (Listener lis : registers) {
            lis.lineAppended(event);
        }
    }

    private class MonitorTask implements Runnable {
        @Override
        public void run() {
            stopRead();

            //why putting the resetReandomAccessFile in this thread method is that it will sleep if the file not exist.
            setRandomFile();
            startRead();
        }

    }

}

This is some help classes:

public interface Listener {
    void lineAppended(FileEvent event);
}


public class FileEvent {
    private String  line;
    private String  source;

    public FileEvent(String filepath, String addedLine) {
        this.line = addedLine;
        this.source = filepath;
    }
    //getter and setter

}

And this is a example to call the monitor:

public class Client implements Listener {
    private static File f   = new File("D:/ab.txt");

    public static void main(String[] args) {
        Monitor.instance.register(f, new Client());
        System.out.println(" I am done in the main method");
        try {
            Thread.sleep(5000);
            Monitor.instance.replaceFile(new File("D:/new.txt"));
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void lineAppended(FileEvent event) {
        String line = event.getLine();
        if (line.length() <= 0)
            return;
        System.err.println("found in listener:" + line + ":" + line.length());
    }
}

Now,my probelm is the code work well if I just call:

Monitor.instance.register(file,listener);

This will monitor the file for line appending,and will notify the listener.

However it does not work when I call the :

Monitor.instance.replaceFile(anotherfile);

This means I want to monitor another file rather than before.

So in my Monitor I have to restart the thread,how to make it?

I have tried the:

monitorThread.interruppt();

It does not wrok.

Anyone can fix it for me or tell me how to do ?

Thanks.

Before I ask,I have googling the "restart java thread",so I know one can not restart a dead thread,but my thread does not return,so I think it can be restarted.


回答1:


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<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();
    }

}



回答2:


You don't restart a Thread, instead you create a new one each time you want to start a thread.

A better alternative may be to use Executors.newCachedThreadPool() which gives you a pool of thread which will be started/recycle for you.

BTW: You are using recursion rather than a loop to poll if the file exists. Using recursion can mean if you wait too long it will throw a StackOverflowError. IMHO you shouldn't wait at all, the polling thread should repeatedly attempt to open the file until it is told to stop (or the file appears)

Your current implementation also means if the file is replaced, you will have to reopen the file in the background thread anyway.




回答3:


See this post How to start/stop/restart a thread in Java?




回答4:


I assume you answered your question

one can not restart a dead thread

This link may be helpful to you How to restart thread in java?




回答5:


A thread in Java cannot be re-started. Every time you need to restart the thread you must make a new one.

That said, you might want to look at:

private void setRandomFile() {
        if (!monitoredFile.exists()) {
            log.warn("File [" + monitoredFile.getAbsolutePath()
                    + "] not exist,will try again after 30 seconds");
            try {
                Thread.sleep(30 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setRandomFile();
            return;
        }
// ....
}

Here you sleep for 30 seconds if the file does not exist, then recursively call the same function. Now, I don't know what business requirements you have, but if this recursion ran long enough you will run out of stack space. Perhaps you will be better served with a while loop or even better, a little synchronisation like a Semaphore.



来源:https://stackoverflow.com/questions/4602758/how-to-restart-a-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!