首页 > 其他分享 >Thread的join方法demo

Thread的join方法demo

时间:2022-10-08 18:24:44浏览次数:53  
标签:capture join Thread successful demo timestamp data

Thread的join方法demo

/**
 * 关于join官方的解释是 Waits for this thread to die. 也就是等待一个线程结束。
 */
public class ThreadJoinTest {
    public static void main(String[] args) throws InterruptedException {
        long startTimestamp = System.currentTimeMillis();

        // 假设有三台机器,开启三个线程。
        Thread m1 = new Thread(new CaptureRunnable("M1", 1_000L));
        Thread m2 = new Thread(new CaptureRunnable("M2", 2_000L));
        Thread m3 = new Thread(new CaptureRunnable("M3", 3_000L));

        /**
         * 可以看到三个线程还没走完,就提前把时间打印出来了,这个不是我我们想要的效果,那么我们让三个线程join一下试试:
         * Save data begin timestamp is 1665222904024, end timestamp is 1665222904025
         * Spend time is 1
         * M1 completed data capture at timestamp [1665222905026] and successful.
         * M2 completed data capture at timestamp [1665222906025] and successful.
         * M3 completed data capture at timestamp [1665222907026] and successful.
         */
        m1.start();
        m2.start();
        m3.start();

        /**
         *
         * 打开注释输出:
         * M1 completed data capture at timestamp [1665222874569] and successful.
         * M2 completed data capture at timestamp [1665222875569] and successful.
         * M3 completed data capture at timestamp [1665222876569] and successful.
         * Save data begin timestamp is 1665222873568, end timestamp is 1665222876569
         * Spend time is 3001
         */
//        m1.join();
//        m2.join();
//        m3.join();

        long endTimestamp = System.currentTimeMillis();

        System.out.printf("Save data begin timestamp is %s, end timestamp is %s\n", startTimestamp, endTimestamp);
        System.out.printf("Spend time is %s\n", endTimestamp - startTimestamp);
    }


    /**
     * 采集服务器节点的任务。
     */
    static class CaptureRunnable implements Runnable {
        // 机器节点的名称
        private String machineName;
        // 采集花费时间
        private long spendTime;

        public CaptureRunnable(String machineName, long spendTime) {
            this.machineName = machineName;
            this.spendTime = spendTime;
        }

        @Override
        public void run() {
            // do the really capture data.
            try {
                Thread.sleep(spendTime);
                System.out.printf(machineName + " completed data capture at timestamp [%s] and successful.\n", System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public String getResult() {
            return machineName + " finish.";
        }
    }

}

 

标签:capture,join,Thread,successful,demo,timestamp,data
From: https://www.cnblogs.com/oktokeep/p/16769795.html

相关文章

  • 【GEE笔记6】数据连接Join
    连接​​ee.Join​​​用于根据​​ee.Filter​​​指定的条件组合来自不同集合(例如ImageCollection或FeatureCollection)的元素。过滤器由每个集合中相互关联的属性的......
  • Demo25_方法的调用
    //方法调用加法典例packagecom.HuanXin.Fan_Fa_5;publicclassDemo01{//main方法publicstaticvoidmain(String[]args){intA=add(3,4);//调用a......
  • Demo26_加强方法调用的理解
    //加强方法调用的理解packagecom.HuanXin.Fan_Fa_5;publicclassDemo02{publicstaticvoidmain(String[]args){CHX();//调用下文CHX()方法}//......
  • packer demo
    ks 应答文件demo installtextrebootlangen_US.UTF-8keyboardustimezone--utcEtc/UTCrootpw--plaintext'kangwen12#$'zerombrautopart--type=plain-......
  • Fairness without Demographics through Adversarially Reweighted Learning
    目录概符号说明本文方法代码LahotiP.,BeutelA.,ChenJ.,LeeK.,ProstF.,ThainN.,WangX.andCHiE.H.Fairnesswithoutdemographicsthroughadversariall......
  • Demo23_or循环与while循环的区别 break与continue的区别
    //for循环与while循环的区别break与continue的区别packagecom.HuanXin.JiBen_JieGou_4;publicclassDemo12_break_continue{publicstaticvoidmain(String[]arg......
  • Demo24_打印三角形
    packagecom.HuanXin.JiBen_JieGou_4;publicclassDemo13_SanJiaoXin{publicstaticvoidmain(String[]args){for(inti=1;i<=5;i++){//总for,i相......
  • Demo22_关于continue
    /continue的理解packagecom.HuanXin.JiBen_JieGou_4;publicclassDemo11_continue{publicstaticvoidmain(String[]args){intA=0;while(A<10......
  • Demo21_关于break
    //break的理解packagecom.HuanXin.JiBen_JieGou_4;publicclassDemo10_break{publicstaticvoidmain(String[]args){for(inti=0;i<100;i++){......
  • hive元起动报错:Exception in thread "main" java.lang.NoSuchMethodError: com.google
    错误原因:1.系统找不到这个类所在的jar包2.jar包的版本不一样系统不知道使用哪个。 hive启动报错的原因是后者解决办法:1、com.google.common.base.Preconditions.che......