During application development so many times we are implementing Threading Concepts but sometimes java directly does not provide and inbuilt functionality that we will notify that thread execution has been complete so like some custom code using that we should know that our thread execution has been completed.

Here is example.

ProcessCompleteListener interface contains complete method which is automatically called by ProcessThread class one thread will complete. In complete() method user can write his/her custom code which will be executed one thread completed.

ProcessCompleteListener.java

package com.threaddemo.thread;

/**
 * Created by Java Developer Zone on 24-03-2017.
 */
public interface ProcessThreadCompleteListener {
        public void complete();
}

 

ProcessThread extends Thread so we will get all functionality of thread in ProcessThread class. Override run method which is calling abstract doRun() method. doRun() method will implemented by use which contains actualy code that shold be perform in thread.

here I have created constructor with one argument of ProcessThreadCompleteListener  interface.

ProcessThread.java

package com.threaddemo.thread;

/**
 * Created by Java Developer Zone on 24-03-2017.
 */
public abstract class ProcessThread extends Thread {
    private final ProcessThreadCompleteListener processThreadCompleteListener;

    public ProcessThread(final ProcessThreadCompleteListener processThreadCompleListener) {
        this.processThreadCompleteListener = processThreadCompleListener;
    }

    @Override
    public void run() {
        try {
            doRun();
        } finally {
            processThreadCompleteListener.complete();
        }
    }

    public abstract void doRun();
}

Demo Application:

User need to create its own class on which extends PrcessThread and need to override doRun method, doRun method will contain actual logic which need to executed inside thread. Once thread execution completed complete method of ProcessThreadCompleteListener will be called.

ProcessThreadDemo.java

package com.threaddemo.thread;

/**
 * Created by Java Developer Zone on 24-03-2017.
 */
public class ProcessThreadDemo extends ProcessThread {
    public ProcessThreadDemo(final ProcessThreadCompleteListener processThreadCompleListener) {
        super(processThreadCompleListener);
    }

    @Override
    public void doRun() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

 ThreadDemo.java

package com.threaddemo.thread;

/**
 * Created by Java Developer Zone on 24-03-2017.
 */
public class ThreadDemo {
    public static void main(String... args) {
        new ProcessThreadDemo(new ProcessThreadCompleteListener() {
            @Override
            public void complete() {
                System.out.println("Process Execution Complete");
            }
        }).start();
    }
}

Output:

0
1
2
3
4
5
6
7
8
9
Process Execution Complete

Refer java-thread , Process & Threads for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *