java-多线程-join函数

join()>>不带参数

线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作

join(int keepTims)>>带参数,与上面类似,区别在于线程B保持并入线程A中有保持时间,超过改时间,两线程再次分开

案例1 

package com.wp.join;

public class JoinTest implements Runnable {
    
    public static int a = 0;
    
    @Override
    public void run() {
        for(;;){
            a++;
            System.out.println("变量A="+a);
            long now = System.currentTimeMillis();
            while (System.currentTimeMillis() - now < 1000) {
            }
            if(a>8){
                break;
            }
        }
    }
    
    public static void main(String[] args) {
        Thread task = new Thread(new JoinTest());
        task.start();
        try {
            task.join(9000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("End var = "+a);
    }
    
}

案例2 

package com.wp.join;

public class JoinTest2 {

    private static int num = 0;

    static class JoinThreadA extends Thread {

        @Override
        public void run() {
            while (true) {
                num++;
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 200) {
                }
                System.out.println("选手A已跑" + num + "米");
                if (num >= 100) {
                    break;
                }
            }
        }
    }

    static class JoinThreadB extends Thread {
        protected JoinThreadA joina;

        public JoinThreadB(JoinThreadA joina) {
            this.joina = joina;
        }

        @Override
        public void run() {
            try {
                joina.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (true) {
                num++;
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 100) {
                }
                System.out.println("选手B已跑" + num + "米");
                if (num >= 200) {
                    break;
                }
            }

        }
    }
    static class JoinThreadC extends Thread {
        protected JoinThreadB joinb;
        
        public JoinThreadC(JoinThreadB joinb) {
            this.joinb = joinb;
        }
        
        @Override
        public void run() {
            try {
                joinb.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (true) {
                num++;
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 50) {
                }
                System.out.println("选手C已跑" + num + "米");
                if (num >= 300) {
                    break;
                }
            }
            
        }
    }
    
    static class JoinThreadD extends Thread {
        protected JoinThreadC joinc;
        
        public JoinThreadD(JoinThreadC joinc) {
            this.joinc = joinc;
        }
        
        @Override
        public void run() {
            try {
                joinc.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (true) {
                num++;
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 20) {
                }
                System.out.println("选手D已跑" + num + "米");
                if (num >= 400) {
                    break;
                }
            }
            
        }
    }
    
    
    public static void main(String[] args) throws Exception {
        System.out.println("比赛开始");
        JoinThreadA joina = new JoinThreadA();
        JoinThreadB joinb = new JoinThreadB(joina);
        JoinThreadC joinc = new JoinThreadC(joinb);
        JoinThreadD joind = new JoinThreadD(joinc);
        joina.start();
        joinb.start();
        joinc.start();
        joind.start();
        joind.join();
        System.out.println("比赛结束,最终跑完" + num + "米");
    }

}

 案例三

package com.wp.join;

import java.text.SimpleDateFormat;
import java.util.Date;

public class JoinTest3 {
    final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    static class JoinThread extends Thread {
        Thread syncThread;

        public JoinThread(Thread syncThread) {
            this.syncThread = syncThread;
        }

        @Override
        public void run() {
            synchronized (syncThread) {
                System.out.println("线程A START");
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 5000) {
                }
                System.out.println("线程A END");
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("开始时间:"+format.format(new Date()));
        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("Begin sleep");
                long now = System.currentTimeMillis();
                while (System.currentTimeMillis() - now < 5000) {
                }
                System.out.println("End sleep");

            }
        });
        Thread t2 = new JoinThread(t1);
        t1.start();
        t2.start();
        try {
            t1.join(1000);//因为T1线程对象的锁被T2持有,所以必须等T2跑完,释放锁 所以此处等待的时间 1000+5000
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束时间:"+format.format(new Date()));
    }

}

 

posted @ 2016-04-05 16:29  匹夫鹏  阅读(257)  评论(0编辑  收藏  举报