Simple Java Multithreading (1)-Method join

Source: Internet
Author: User
Tags join sleep thread

For Java developers, multithreading should be a knowledge point that must be expertly applied, especially in the development of Java-language based products. This article will explain the Java multithreading knowledge, in the follow-up series will focus on Java5 by Doug Lea Professor Concurrent Parallel package design idea and concrete implementation and application.

How can I understand the simple, my understanding is with the question, but not the general look. So the series is basically to solve the problem, of course, I also very much hope that readers can put forward a better solution to the problem and ask more questions. Because the level is limited, if there is anything wrong, please put forward, to discuss, in short, I hope that through the series we can in-depth understanding of Java multithreading to solve our actual development problems.

As a developer, I don't think it's necessary to discuss the basics of multithreading, like what is a thread? How to create and so on, these knowledge points can be obtained through books and Google. This series is mainly how to solve multithreading to help us in peacetime development, such as thread pool how to achieve? How to apply locks and so on.

(1) What does the method join do? Simple answer, sync, how to sync? How did it happen? The following will be answered individually.

Since the touch of Java multithreading, the join has not been understood. This is what the JDK says:

join
   public final void join(long millis)throws InterruptedException
   Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

Can you understand that? The literal meaning is waiting for a while until this thread dies, my question is whether that thread is its own thread or the thread that invoked it, on the code:

package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
   public static void main(String[] args) {
     Thread t = new Thread(new RunnableImpl());
     t.start();
     try {
       t.join(1000);
       System.out.println("joinFinish");
     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
}
class RunnableImpl implements Runnable {
   @Override
   public void run() {
     try {
       System.out.println("Begin sleep");
       Thread.sleep(1000);
      System.out.println("End sleep");
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
}

The result:

Begin sleep
End sleep
joinFinish

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.