Java implements the main thread waiting for sub-threads

Source: Internet
Author: User

This article describes the implementation of two main threads waiting for sub-threads, which are described as five sub-threads:

1. Using the join () method of Thread, the join () method will block the main Thread from continuing to execute downward.

2. Using CountDownLatch in java. util. concurrent is a reciprocal counter. During initialization, an initial countDown value is set. Each time the countDown () method is called, the inverse value is reduced by one. The await () method blocks the current process until it is counted to 0.

The join method code is as follows:

package com.test.thread;import java.util.ArrayList;import java.util.List;public class MyThread extends Thread{    public MyThread(String name)    {        this.setName(name);    }    @Override    public void run()    {        System.out.println(this.getName() + " staring...");        System.out.println(this.getName() + " end...");    }    /**     * @param args     */    public static void main(String[] args)    {        System.out.println("main thread starting...");        List
 
   list = new ArrayList
  
   ();        for (int i = 1; i <= 5; i++)        {            MyThread my = new MyThread("Thrad " + i);            my.start();            list.add(my);        }        try        {            for (MyThread my : list)            {                my.join();            }        }        catch (InterruptedException e)        {            e.printStackTrace();        }        System.out.println("main thread end...");    }}
  
 
The running result is as follows:

Main thread starting...
Thrad 2 staring...
Thrad 2 end...
Thrad 4 staring...
Thrad 4 end...
Thrad 1 staring...
Thrad 1 end...
Thrad 3 staring...
Thrad 3 end...
Thrad 5 staring...
Thrad 5 end...
Main thread end...
The code for CountDownLatch is as follows:

package com.test.thread;import java.util.concurrent.CountDownLatch;public class MyThread2 extends Thread{    private CountDownLatch count;    public MyThread2(CountDownLatch count, String name)    {        this.count = count;        this.setName(name);    }    @Override    public void run()    {        System.out.println(this.getName() + " staring...");        System.out.println(this.getName() + " end...");        this.count.countDown();    }    /**     * @param args     */    public static void main(String[] args)    {        System.out.println("main thread starting...");        CountDownLatch count = new CountDownLatch(5);        for (int i = 1; i <= 5; i++)        {            MyThread2 my = new MyThread2(count, "Thread " + i);            my.start();        }        try        {            count.await();        }        catch (InterruptedException e)        {            e.printStackTrace();        }        System.out.println("main thread end...");    }}

The running result is as follows:

Main thread starting...
Thread 2 staring...
Thread 2 end...
Thread 4 staring...
Thread 4 end...
Thread 1 staring...
Thread 1 end...
Thread 3 staring...
Thread 3 end...
Thread 5 staring...
Thread 5 end...
Main thread end...


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.