Pawan
Pawan

Reputation: 32331

Whether using Threads join correctly or not

I Want to execute a Certain Task to take only 1000 MS , if it exceeds , i dont want to continue with the task , i have used join for this .

Please tell me and guide me if this is correct or not

import java.util.List;

public class MainThread {

    public static void main(String args[]) throws InterruptedException {
        Thread mainthread = Thread.currentThread();
        ChildThread child = new ChildThread();
        Thread childThread = new Thread(child);
        childThread.start();

        mainthread.join(1000);
        List list = child.getData();

        if(list.size()<0)

        {
            System.out.println("No Data Found");
        }
    }
}

ChildTHread

import java.util.ArrayList;
import java.util.List;

public class ChildThread implements Runnable

{

    List list = new ArrayList();

    public List getData() {
        return list;
    }

    public void run() {
        // This List Data is feteched from Database currently i used some static data
        list.add("one");
        list.add("one2");
        list.add("one3");

    }
}

Upvotes: 1

Views: 178

Answers (4)

mre
mre

Reputation: 44240

Interrupting a thread is the more common (and better) approach. If you want the task to execute for at most 1 second and then stop, use Thread.interrupt(), otherwise the thread will continue to run. It is important to note that depending on how your actual code is structured, you may need to propagate the interrupt.

EXAMPLE

public class Demo {
    public static void main(String[] args){
        final List<String> list = new ArrayList<String>(3);

        final Thread t = new Thread(new Runnable(){
            @Override
            public void run() {
                synchronized(list){
                    list.add("one");
                    list.add("one2");
                    list.add("one3");
                }
            }
        }, "DemoThread");
        t.start();

        try {
            t.join(1000);
            t.interrupt();
        } catch (InterruptedException e) {
            // handle exception
        }

        synchronized(list){
            if(list.isEmpty()){
                System.out.println("No data found");
            }else{
                System.out.println(list);
            }
        }
    }
}

Upvotes: 1

Troydm
Troydm

Reputation: 2670

yup that's the way, the only problem i see is in ChildThread list, i suggest you to use synchronized method like this so that you won't have race conditions

List list = Collections.synchronizedList(new ArrayList());

also if you want the running thread to be stoped if it's execution time exceeded 1000 ms i suggest you to use interrupt method of Thread object and don't forget to catch interrupt exception in child thread so you won't have unnecessary exceptions in log

Upvotes: 0

Jarek Potiuk
Jarek Potiuk

Reputation: 20077

Nope. Incorrect. You do not need MainThread at all, you should call childThread.join(1000) instead.

But there is a problem with this approach as well - it will mean that the child thread will anyhow continue to be running. Therefore you should call also childThread.interrupt() after join:

childThread.join(1000);
childThread.interrupt();

and in your child thread periodically in your childThread perform something like that:

if (interrupted()) {
   return;
}

and handle InterruptedException where needed - usually around any wait() methods you have.

Upvotes: 3

Denis Tulskiy
Denis Tulskiy

Reputation: 19177

No, this will not work, because this code will wait for one second, and if the thread is not finished, it will just go on. The thread will continue to run. Call thread.interrupt() to interrupt the thread, or close the connection so that it throws an exception and stops the thread, and then join on the thread.

Upvotes: 0

Related Questions