zoukankan      html  css  js  c++  java
  • 多线程Demo

    thread.join() 方法主要是让调用该方法的thread完成run方法里面的东西后, 再执行join()方法后面的代码

    下面是一个简单版的例子,通过继承Thread 实现同步和异步多线程

    同步

    /**
     * 同步
     */
    public class SynThread extends Thread {
        private int num;
     
        public SynThread(int num) {
            this.num = num;
        }
     
        @Override
        public void run() {
            //保证同步,因为使用了thread.sleep,所以必须对整个thread类加锁,否则的话只需对将run()方法改为 public synchronized void run()
            synchronized (SynThread.class) {
                System.out.println("syn thread:" + num + " start");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("syn thread:" + num + " end");
            }
        }
    }
    

      异步

    /**
     * 异步
     */
    public class AsynThread extends Thread{
        private int num;
        public  AsynThread(int num){
            this.num=num;
        }
        @Override
        public void run() {
            System.out.println("asyn thread:" + num+" start");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("asyn thread:" + num+" end");
        }
    }
    

     同步和异步多线程

    import java.util.ArrayList;
    import java.util.List;
     
    public class MultiThreadDemo {
        public static void testMultiSynThread(int num){
            System.out.println("等待子线程完成:");
            List<SynThread> threadList = new ArrayList<SynThread>();
            for (int i = 1; i < num; i++) {
                SynThread thread = new SynThread(i);
                threadList.add(thread);
                thread.start();
            }
            //使用thread.join()等待子线程完成
            for (SynThread thread : threadList) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
     
        public static void testMultiAsynThread(int num){
            System.out.println("等待子线程完成:");
            List<AsynThread> threadList = new ArrayList<AsynThread>();
            for (int i = 1; i < num; i++) {
                AsynThread thread = new AsynThread(i);
                threadList.add(thread);
                thread.start();
            }
            //使用thread.join()等待子线程完成
            for (AsynThread thread : threadList) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
     
        public static void main(String[] args) {
     
            System.out.println("同步多线程开始:");
            long start=System.currentTimeMillis();
            testMultiSynThread(1000);
            long end=System.currentTimeMillis();
            System.out.println("cost time:"+(end-start));
            System.out.println("异步多线程开始:");
            start=System.currentTimeMillis();
            testMultiAsynThread(1000);
            end=System.currentTimeMillis();
            System.out.println("cost time:"+(end-start));
     
        }
    }
    

      

    最后跑了下有10个线程的时候,同步和异步花费的时间,当然每次跑的结果都不一样,但是异步比同步快,因为同步多线程中必须一个线程的run中的代码执行完后才能执行下一个线程

    syn cost time:9005

    asyn cost time:1005

     

  • 相关阅读:
    CSS3 盒模型
    前端Vue框架使用思路
    request+正则爬猫眼电影榜top100
    最大似然估计(Maximum likelihood estimation)
    pandas-resample按时间聚合
    Sklearn调参之sklearn.model_selection.GridSearchCV
    sklearn.metrics中的评估方法介绍(accuracy_score, recall_score, roc_curve, roc_auc_score, confusion_matrix,classification_report)
    【vivado】安装丢失组件和无法启动
    【电路】连接器选型
    【IP分析】合并信号concat,拆分总线slice
  • 原文地址:https://www.cnblogs.com/zhu-tingting/p/7508461.html
Copyright © 2011-2022 走看看