3

This is my code:

Thread T = new Thread() {
    public void run() {
        //some code here...
    }
};
T.start();

I need to know when this specific thread has finished so I can start another code right afterwards, something like this hypothetical option:

T.finished(){
    //some code here
}

EDIT: the solution provided by @swpalmer is the most straightforward and the easiest compared to all the other ones, I would not consider this being a duplicate as the accepted solution is different to the others and is really extremely easy to implement, yet doing what it should and what I was asking for!

qraqatit
  • 492
  • 4
  • 14

3 Answers3

4

Here:

 public void run() {
    //some code here...
    whatever.finished();
 }

That is the straight forward way of solving this: your last action in your thread is to send that "event".

Sure, you can also use Thread.join() to do that from the outside.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    in fact you inspired me to realize there is try() finally{} which for now I am using but still waiting for more exact solution, as I think this is still inside the T thread as its last action but still inside and I need to start my other code outside the T thread when it ends – qraqatit Sep 11 '19 at 20:35
3

Instead of Thread, consider using java.util.concurrent.CompletionStage

You can chain together actions using whenComplete() or one of the other methods.

See examples here

    CompletableFuture.runAsync(() -> {
        //some code here...
        System.out.println("Hi!");
    }).thenRun(() -> {
        //some code here
        System.out.println("Greeting completed");
    });
swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • thanks for the link but my head goes round from that messy examples webpage so I am not wiser from there, actually I am quite newbie still...please, can you post example based on my code? – qraqatit Sep 11 '19 at 20:29
  • See revised answer. – swpalmer Sep 11 '19 at 20:37
0

Ah, amazing question. What you want is a callback - a method that needs to be pushed to a thread's call stack when some previous task is done. A crude way to do this is to define a new task (using a Runnable or a Callable) after your task:

Thread T = new Thread() {
    public void run() {
        //after this code finished
        Runnable callback = () -> {//some task};
        ES.submit(callback);    //ES = ExecutorService
    }
};

Now supposing you want to execute a task when the second task is complete. You would do something like:

Thread T = new Thread() {
    public void run() {
        //after this code finished
        Runnable callback = () -> {
            //task completed
            //second callback
            Runnable callback2 = () -> {};
            ES.submit(callback2);
        };
    }
};

You see the code is getting hideous and non-idiomatic (the notorious 'Callback Hell'). To counter this, Java 8 introduced the CompleteableFuture<T> class. You can basically get a CompleteableFuture<T> as a promise and then apply methods like:

promise.thenApplyAsync(callback).thenApplyAsync(callback)...

CompletableFutures<T> are literally Javascript promises - they can be either in pending stage, or complete, or complete with an error.

Reactive streams are the next step. But of course, to understand it, you need to understand the above things properly.

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44