Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Wait for third party threads to finish

Tags:

java

I have a Thread that will run a third party lib which also will run their own Threads. When the run method of my Thread finishes, third party Threads will not be finished yet.

So, what is the best way to hold my Thread until these external Threads are still running?

like image 626
bsferreira Avatar asked Dec 17 '14 01:12

bsferreira


People also ask

Does Java wait for all threads to finish?

Behind the scenes it is using default JVM's fork join pool which means that it will wait for all the threads to finish before continuing.

How do I make main thread wait for other threads?

The statement “Thread. currentThread(). join()”, will tell Main thread to wait for this thread(i.e. wait for itself) to die.

How do you make a thread wait for some time?

In between, we have also put the main thread to sleep by using TimeUnit. sleep() method. So the main thread can wait for some time and in the meantime, T1 will resume and complete its execution.

Which method wait for a Java thread to finish?

The wait() and join() methods are used to pause the current thread. The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.


1 Answers

If you are an application and don't have to worry about SecurityManager restrictions, and if you are prepared to occasionally modify your code when the third-party code is updated, you can use the facilities of ThreadGroup to walk the threads and identify them by name or by the thread group that contains them.

Once you've located the threads it's a simple job to either monitor them until they are done or to use Thread.join() as appropriate.

As an example, here is some working code which dumps all threads in the JVM:

public void printThreads(PrintWriter wtr) {
    ThreadGroup     root;

    totGroups=0;
    totThreads=0;

    for(root=Thread.currentThread().getThreadGroup(); root.getParent()!=null; root=root.getParent()) {}
    wtr.println("Thread Dump:");
    printThreadGroup(wtr,root,"  ");
    wtr.println("  -- Total Groups: "+totGroups+", Total Threads: "+totThreads);
    }

public void printThreadGroup(PrintWriter wtr, ThreadGroup grp, String pfx) {
    try {
        Thread[]        thds;
        ThreadGroup[]   grps;

        totGroups++;
        wtr.println(pfx+"Group: "+grp.getName()+", "+(grp.isDaemon()?"Daemon":"Normal")+", "+(grp.isDestroyed()?"Destroyed":"Alive")+", "+grp.getMaxPriority());
        thds=new Thread[grp.activeCount()];
        grp.enumerate(thds,false);
        Arrays.sort(thds,THREAD_SORTER);
        for(int xa=0; xa<thds.length && thds[xa]!=null; xa++,totThreads++) {
            Thread          thd=thds[xa];
            wtr.println(pfx+". - ["+thd.getName()+", "+(thd.isDaemon()?"Daemon":"Normal")+", "+(thd.isAlive()?"Alive":"Not Started or Dead")+", "+thd.getPriority()+"]");
            }

        grps=new ThreadGroup[grp.activeGroupCount()];
        grp.enumerate(grps,false);
        Arrays.sort(grps,GROUP_SORTER);
        for(int xa=0; xa<grps.length && grps[xa]!=null; xa++) {
            printThreadGroup(wtr,grps[xa],(pfx+". "));
            grps[xa]=null;
            }
        }
    catch(Throwable thr) {
        wtr.println("  Cannot print threads ("+thr+")");
        }
    }

public void printStacks(PrintWriter wtr) {
    wtr.println("Thread Stack Traces:");
    try { javaMx.printStacks(wtr); } catch(Throwable thr) { wtr.println("  Cannot print stacks ("+thr+")"); }
    wtr.println("  --");
    }
like image 145
Lawrence Dol Avatar answered Nov 06 '22 08:11

Lawrence Dol