How to restart thread in java?

16,683

Solution 1

If a thread is dying due to an uncaught exception, the answer is simple: catch the exception at an appropriate place so that you can keep going. Either catch the exception within your searchfile method, or make the run method call searchfile in a loop.

Solution 2

If you want your thread to keep running use a loop.

public void run() {
   while(!Thread.interrupted())
      try {
           searchfile();
      }
      catch(Exception e) {
          e.printStackTrace();
      }
}

Solution 3

Inside your catch, you can move the file to the error folder then create a new object of the same thread and start it again.

Solution 4

I'm not entirely sure if this will work, yet here's a try.

public void run() {
    try {
        searchFile();
    } catch(Exeption e) {
        e.printStackTrace();
        if(!Thread.currentThread().isAlive())
            Thread.currentThread().start();
    }
}

Solution 5

unless i got you wrong, your code is missing the "keep running" nature, i.e. you need to have a loop somewhere:

public static void main(String[] args){

    ExecutorService service = Executors.newFixedThreadPool(4);

    // for each of your 4 folders
    while (true) {
        Future<File> searchResult = service.submit(new SearchTask());
        try {
          File foundFile = searchResult.get();
          // handle found file
        } catch (Exception e) {
          //    handle exception
        }
    }
}

private static class SearchTask implements Callable<File> {

    @Override
    public File call() {
      return searchFile();
    }

    public File searchFile() {
      // search & return found file
    }

}

note that this is just a very simple extension of your example. it is still missing the parametrization of the SearchTask to actually be specific for a folder, handling of files & exceptions, etc. as mentioned in previous answers, your SearchTask should implement Runnable (i prefer Callable...), and IMHO it's always better to use an ExecutorService than to spawn threads manually. hope this helps...

Share:
16,683
raja
Author by

raja

Updated on June 18, 2022

Comments

  • raja
    raja almost 2 years

    I have created a program which searches for files in a source folder. If it finds any file, it processes that file and moves it to a destination folder, then looks for a new file in the source folder. It has to keep on checking the source folder for a file.

    I have used a thread to look for files in the source folder. The problem I am facing is whenever any exception is thrown during file processing, the thread gets stopped. I want the thread to be running even if an exception is thrown. It has to move the file that caused the error to some other folder and look for a new file in the source folder. How can I make the thread keep on running?

    Eg:

    public void run() {
        try {
            searchfile();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public void searchfile(){
      ...
    }
    

    Update :

    I should be more clear in my question. Actually there are 4 source folders and 4 destination folders. I have to perform the same operation in each source & destination pair. So i have created 4 threads in one class and do the operation in separate class.

    class MainClass
    {
       public static void main(String[] args){
          for(int i=0;i<4;i++){
             SearchClass search = new SearchClass();
             Thread thread = new Thread(search);
             thread.start();
         }
       }   
    }
    
    class SearchClass
    {
       public void run() {
       try {
          searchfile();
       } catch(Exception e) {
          e.printStackTrace();
       }
    }
    
    public void searchfile(){ ... } }
    

    All the thread wont stop running eventhough it caught any exception in middle. How can i do that?