Father and son's story takes you to understand Java threads

Source: Internet
Author: User
Tags string tags

Today, thinking back to threading knowledge, found a very interesting small program. is used to explain the multithreading of the following post-sharing, for people who have just started learning to understand the thread has very great help

The story of father and son

<span style= "font-family:kaiti_gb2312;font-size:18px;" >public class Fatherthread extends thread{@Override public void Run () {System.out.println ("Dad wants to smoke 。          Found the smoke was exhausted ");           System.out.println ("Father let son to buy bag Hongtashan");            Thread son = new Sonthread ();              Son.start ();            System.out.println ("Daddy waits for his son to buy cigarettes back");              try {//join meaning: Wait for the son thread to finish running, Father line friend continue to run Son.join ();              } catch (Interruptedexception e) {System.out.println ("where Dad went out to find his son");          System.exit (1);                } System.out.println ("Dad was happy to take the cigarette and give the change to his son"); }} </span> 
<span style= "font-family:kaiti_gb2312;font-size:18px;"          >public class Sonthread extends thread{@Override public void Run () {String tags = "\t\t\t\t\t";          System.out.println (tags+ "son went to buy cigarettes");          System.out.println (tags+ "Son to buy cigarettes for 10 minutes");                  try {for (int i =0; i<10;) {thread.sleep (1000);              System.out.println (tags+ "son out of the first" + ++i + "Minutes");                        }} catch (Interruptedexception e) {e.printstacktrace ();      } System.out.println (tags+ "son went to buy cigarettes back"); }} </span> 
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public class Main {public      static void Main (string[] args) {          System.out.println ("Story of Papa and Son");          Thread fathread =new fatherthread ();          Fathread.start ();          try {              thread.sleep;          } catch (Interruptedexception e) {              //TODO auto-generated catch block              E.printstacktrace ();  }}} </span>


Execution Results
The story of father and son
Dad wanted to smoke and found the smoke was over.
Dad asked his son to buy a bag Hongtashan
Dad waits for his son to buy cigarettes.
Son went to buy cigarettes.
It takes 10 minutes for my son to buy cigarettes.
Son out for the 1th minute
Son out for the 2nd minute
Son out for the 3rd minute
Son out for the 4th minute
Son out for the 5th minute
Son out for the 6th minute
Son out for the 7th minute
Son out for the 8th minute
Son out for the 9th minute
Son out for the 10th minute
Son went to buy cigarettes and came back.
Dad was happy to take the cigarette and gave the change to his son.
  program enters the main function, first Father thread starts to run. Father asked his son to buy cigarettes, and then waited for his son to buy cigarettes back. At this point the son thread starts 10 minutes after the boy returns Fathrer thread continues to run.



Father and son's story take you to understand Java threads

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.