Java – wait for the third-party thread to complete

I have a thread running a third-party library, which will also run its own thread When the run method of my thread completes, the third-party thread will not complete

So, what is the best way to keep my threads running until these external threads are still running?

resolvent

Solution

If you are an application and don't have to worry about securitymanager restrictions, and if you plan to modify the code occasionally when updating the third-party code, you can use the tools of ThreadGroup to traverse the threads and identify them in the following ways: name or thread group containing them

Once threads are found, you can monitor them before they are complete, or use thread when appropriate join().

For example, here are some working code dumps for 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("  --");
    }

The above is the Java - waiting for the third-party thread to complete all the contents collected by the programming home for you. I hope this article can help you solve the program development problems encountered by Java - waiting for the third-party thread to complete.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>