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?
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.
The statement “Thread. currentThread(). join()”, will tell Main thread to wait for this thread(i.e. wait for itself) to die.
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.
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.
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(" --");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With