Pri_stack
Pri_stack

Reputation: 189

How do we come to know that the thread has finished its execution?

I am using ExecutorService. Following is the code

public class A{ 
   public static void main(String args[]){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Runnable worker = new FileUploadThread("thread");
        executorService.execute(worker);
   } 
}

public class FileuploadThread extends Thread{
    //has a parametrized constuctor

     @Override
     public void run(){
        for(int i=0; i<10000; i++){
            syso("executing...");
        }
     }
}

I want to receive an event or something in the main method when the thread completes it task. How can i do that ?

Upvotes: 1

Views: 78

Answers (3)

pbajpai
pbajpai

Reputation: 1369

To know about the task status - You need Future instance. Now there are two points:

  1. If you are just interested to know whether the task has been completed or not, Use executorService.submit(worker) , instead of executorService.execute(worker) method.
  2. If you also want to get some result back after the task completion, Use Callable interface instead of Runnable. See below code:

    public class A {
      public static void main(String args[]){
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Callable<String> worker = new FileUploadThread("thread");
        Future<String> workerTask = executorService.submit(worker);
    
        try {
            boolean isDone = workerTask.isDone();
            System.out.println("Task is done: " + isDone);
    
            //Wait untill task is executing
            String status = workerTask.get();
    
            System.out.println("Status: " + status);
            isDone = workerTask.isDone();
            System.out.println("Task is done: " + isDone);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
      }
    }
    
    class FileUploadThread implements Callable<String> {
      //has a parametrized constuctor
      public FileUploadThread(String thread) { }
    
      @Override
      public String call() throws Exception {
        for(int i=0; i<5; i++){
            System.out.println("executing..sleep for 1 sec...");
            Thread.sleep(1000);
        }
        return "DONE";
      }
    }
    

Output:

Task is done: false
executing..sleep for 1 sec...
executing..sleep for 1 sec...
executing..sleep for 1 sec...
executing..sleep for 1 sec...
executing..sleep for 1 sec...
Status: DONE
Task is done: true

Upvotes: 4

fastcodejava
fastcodejava

Reputation: 41097

You could use yourThread.join().

Upvotes: 0

user1234SI.
user1234SI.

Reputation: 1860

When you start a thread with yourThread.start(), the system starts a new thread doing what is inside the run method. You can simply print something like this : System.out.println("FINISHED " + this.getName()) at the end of the run method.

PS : please make sure you don't place the print above in some loops, it should be the last statement in the run method. ( The .execute() method is almost the same thing with .start() )

Upvotes: 0

Related Questions