como reiniciar un hilo

Traté de escribir un monitor de archivo que verificará el archivo si se agrega una nueva línea, el monitor de hecho es un hilo que leerá la línea por un archivo de acceso aleatorio todo el tiempo.

Estos son los códigos principales del monitor:

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

    }

}

Estas son algunas clases de ayuda:

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

}

Y este es un ejemplo para llamar al 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());
    }
}

Ahora, mi problema es que el código funciona bien si solo llamo:

Monitor.instance.register(file,listener);

Esto monitoreará el archivo para agregar líneas y notificará al oyente.

Sin embargo, no funciona cuando llamo a:

Monitor.instance.replaceFile(anotherfile);

Esto significa que quiero monitorear otro archivo en lugar de antes.

Entonces, en mi monitor, tengo que reiniciar el hilo, ¿cómo hacerlo?

He intentado el:

monitorThread.interruppt();

No funciona.

¿Alguien puede arreglarlo por mí o decirme cómo hacerlo?

Gracias.

Antes de preguntar, busqué en Google el "reinicio del hilo de Java", así que sé que uno no puede reiniciar un hilo muerto, pero mi hilo no regresa, así que creo que se puede reiniciar.

Respuestas a la pregunta(5)

Su respuesta a la pregunta