天天看点

多线程同步-主线程等待所有子线程完成案例

http://blog.csdn.net/qiujuer/article/details/34862469

有时候我们会遇到这样的问题:做一个大的事情可以被分解为做一系列相似的小的事情,而小的事情无非就是参数上有可能不相同而已!

此时,如果不使用线程,我们势必会浪费非常多的时间来完成整个大的事情,而使用线程的话将会存在这样的问题:

主线程启动所有子线程并发执行后主线程就直接返回了,导致外部函数判读整个大的事情完成了,但是实际上并没有完成!

针对以上情况我想我会采用多线程方式执行同时解决主线程等待子线程的问题。如图:

多线程同步-主线程等待所有子线程完成案例

在这里我使用java进行案例分析。

首先建立一个线程管理类,用于启动所有子线程和等待所有子线程完成,在这里不使用休眠一段时间后循环检测的方式(消耗cup同时消耗时间,全部完成时间不够及时等缺点);而是使用等待临界值的方式。threadmanager.java如下:

[java] view

plaincopy

多线程同步-主线程等待所有子线程完成案例
多线程同步-主线程等待所有子线程完成案例

public class threadmanager implements notifyinterface {  

    private final object mlock = new object();  

    private int mcount = 0;  

    private int endcount = 0;  

    public threadmanager(int count) {  

        system.out.println("manager in.");  

        this.mcount = count;  

        this.addthread();  

        synchronized (mlock) {  

            while (true) {  

                if (checkend())  

                    break;  

                try {  

                    mlock.wait();  

                } catch (interruptedexception e) {  

                    e.printstacktrace();  

                }  

            }  

        }  

        system.out.println("manager out.");  

    }  

    private void addthread() {  

        system.out.println("manager addthread().");  

        for (int i = 1; i <= mcount; i++) {  

            threaddothing dthread = new threaddothing(i, "t" + i, this);  

            // start  

            dthread.start();  

    private boolean checkend() {  

        boolean bflag = false;  

        bflag = endcount >= mcount;  

        system.out.println("manager checkend().return is:" + bflag);  

        return bflag;  

    @override  

    public void runend() {  

            ++endcount;  

            mlock.notifyall();  

}  

此类集成自:notifyinterface接口,notifyinterface是用于子线程通知主线程自己已经完成工作所用类,threadmanager实例化时将传入一个int值,用于设置启动的子线程数,当然这里是为了简单介绍所以采用的这样的方式,实际情况可能更加复杂。

在实例化后  进入构造方法,此时将会启动子线程,启动后进入循环等待中,当检测到所有子线程完成时就退出循环,没有就将进入临界值等待,直到通过接口通知主线程完成时将会通知临界值一次,此时循环将会执行一次,如果不满足退出条件将继续等待临界值。直到满足为止。

notifyinterface接口如下:

多线程同步-主线程等待所有子线程完成案例
多线程同步-主线程等待所有子线程完成案例

public interface notifyinterface {  

    public abstract void runend();  

测试用的子线程threaddothing.java如下:

多线程同步-主线程等待所有子线程完成案例
多线程同步-主线程等待所有子线程完成案例

public class threaddothing extends thread {  

    private notifyinterface minterface = null;  

    private int mid = 0;  

    private string margs = null;  

    public threaddothing(int id, string args, notifyinterface iface) {  

        this.mid = id;  

        this.margs = args;  

        this.addinterface(iface);  

    public void addinterface(notifyinterface iface) {  

        this.minterface = iface;  

    public void run() {  

        system.out.println("threaddothing id is:" + this.mid + " args is:" + this.margs);  

        system.out.println(this.mid + ":doing...");  

        int sleeptime = (int) (math.random() * 1000);  

        try {  

            thread.sleep(sleeptime);  

        } catch (interruptedexception e) {  

            e.printstacktrace();  

        system.out.println(this.mid + ":sleeptime is:" + sleeptime);  

        this.notifyend();  

        system.out.println(this.mid + ":do end.");  

    private void notifyend() {  

        if (this.minterface != null)  

            this.minterface.runend();  

        system.out.println(this.mid + ":notify end.");  

此类继承自thread类,可直接重写run()方法完成所做工作!

在工作中,我使用了随机一个1s内的休眠来代替所做工作的时间,完成后调用接口通知完成。

测试方法如下:

多线程同步-主线程等待所有子线程完成案例
多线程同步-主线程等待所有子线程完成案例

/** 

 * @param args 

 */  

public static void main(string[] args) {  

    // todo auto-generated method stub  

    threadmanager manager = new threadmanager(10);  

测试结果:

[plain] view

多线程同步-主线程等待所有子线程完成案例
多线程同步-主线程等待所有子线程完成案例

manager in.  

manager addthread().  

threaddothing id is:1 args is:t1  

threaddothing id is:2 args is:t2  

2:doing...  

1:doing...  

threaddothing id is:3 args is:t3  

threaddothing id is:4 args is:t4  

3:doing...  

4:doing...  

threaddothing id is:5 args is:t5  

5:doing...  

threaddothing id is:6 args is:t6  

manager checkend().return is:false  

threaddothing id is:8 args is:t8  

threaddothing id is:7 args is:t7  

8:doing...  

threaddothing id is:9 args is:t9  

9:doing...  

6:doing...  

threaddothing id is:10 args is:t10  

7:doing...  

10:doing...  

3:sleeptime is:111  

3:notify end.  

3:do end.  

5:sleeptime is:142  

5:notify end.  

5:do end.  

4:sleeptime is:199  

4:notify end.  

4:do end.  

7:sleeptime is:342  

7:notify end.  

7:do end.  

10:sleeptime is:346  

10:notify end.  

10:do end.  

6:sleeptime is:397  

6:notify end.  

6:do end.  

9:sleeptime is:468  

9:notify end.  

9:do end.  

1:sleeptime is:475  

1:notify end.  

1:do end.  

2:sleeptime is:686  

2:notify end.  

2:do end.  

8:sleeptime is:828  

8:notify end.  

manager checkend().return is:true  

8:do end.  

manager out.  

实际情况可能更加复杂,甚至子线程下还有更多的子线程!

具体情况大家可以衍生考虑,检测是否全部返回也可以有多种方式甚至设置添加一个定时器之类的。

以后有时间画一个详细点的图!