简体   繁体   中英

Is there usage for the main thread not calling `join()` to wait for a created thread to finish in Java?

In Java, a main thread creates and starts another thread. If the main thread does not call join() to wait for the new thread to finish, the new thread will continue running after the main thread exits. Is there some usage for the main thread not calling join() ?

For comparison to Linux, I learned from APUE that when a program fork() sa child process and doesn't call waitpid() to wait for the child process to finish but exits while the child continues to run, we can

  • re-parent the child to be adopted by init process (which can prevent it become a zombie process), and

  • make the child not a leader of any process group or process session, so that the child can call setsid() to disassociate from its controlling terminal (which can make the child a daemon process)

Is it correct that the above two benefits don't apply to the Java threads?

Thanks.

Any Java application implicitly join s all non-daemon threads before exiting so essentially, the main thread does join all running threads. See here for a little more detail.

In Java there is no explicit need for a process/thread to have an owning/parent thread. All threads are just start ed and run to completion or until they are interrupted.

Not familiar with Linux but it sounds like the Linux scenarios you describe are similar to the daemon thread mechanisms in Java.

one usage is when you handle a http request from a servlet which has lots of processing and need to response back confirmation of receiving data before request time out .

        import java.io.IOException;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;

  /**
  * Servlet implementation class Processing
   */
   @WebServlet("/Processing")
  public class Processing extends HttpServlet {
      private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Processing() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    new Thread(new Runnable() {

        @Override
        public void run() {
            // do the heavy data processing which takes lots of time.lets assume large
            //amount of data to be added to data base.

        }

    }).start();
    //response back to client by informing data received before request //time out on client side
    response.getWriter().write("request receive sucessfully");
}

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM