多线程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

 

原文地址:https://www.cnblogs.com/zhu-tingting/p/7508461.html