多线程编程例子


title: 多线程编程例子
date: 2019-10-1 19:00:08
updated: 2020-02-27 19:04:06
categories: 多线程
tags:
- 多线程


本博文记录多线程编程的几个案例,包含了常见的几种多线程编程套路。

1.四个线程计算1到100的求和

1.依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步

public class ThreadTest {
   
    private static Object LOCK = new Object();
    private static int sum = 0;
    public static void main(String[] args) throws InterruptedException {
   
        ThreadTest threadTest = new ThreadTest();
        Thread t1 = threadTest.new ThreadSum(1,25);
        Thread t2 = threadTest.new ThreadSum(26,50);
        Thread t3 = threadTest.new ThreadSum(51,75);
        Thread t4 = threadTest.new ThreadSum(76,100);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        System.out.println("totalSum: " + sum);
    }
    class ThreadSum extends Thread{
   
        private int begin;
        private int end;
        public ThreadSum(int begin, int end){
   
            this.begin = begin;
            this.end = end;
        }

        @Override
        public void run() {
   
            try {
   
                Thread.sleep(100);//便于测试
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            synchronized (LOCK){
   
                for(int i=begin; i<=end; ++i){
   
                    sum += i;
                }
                System.out.println(Thread.currentThread().getName() + ": " + sum);
            }

        }
    }
}
out:Thread-1: 950
Thread-0: 1275
Thread-3: 3475
Thread-2: 5050
totalSum: 5050

2.子线程执行完调用countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步

public class ThreadTest {
   
    private static Object LOCK = new Object();
    private static int sum = 0;
    private static CountDownLatch countDownLatch = new CountDownLatch(4);
    public static void main(String[] args) throws InterruptedException {
   
        ThreadTest threadTest = new ThreadTest();
        Thread t1 = threadTest.new ThreadSum(1,25);
        Thread t2 = threadTest.new ThreadSum(26,50);
        Thread t3 = threadTest.new ThreadSum(51,75);
        Thread t4 = threadTest.new ThreadSum(76,100);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        countDownLatch.await();

        System.out.println("totalSum: " + sum);
    }
    class ThreadSum extends Thread{
   
        private int begin;
        private int end;
        public ThreadSum(int begin, int end){
   
            this.begin = begin;
            this.end = end;
        }

        @Override
        public void run() {
   
            try {
   
                Thread.sleep(100);//便于测试
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            synchronized (LOCK){
   
                for(int i=begin; i<=end; ++i){
   
                    sum += i;
                }
                System.out.println(Thread.currentThread().getName() + ": " + sum);
            }
            countDownLatch.countDown();
        }
    }
}

3.线程池

public class ThreadTest {
   
    private static Object LOCK = new Object();
    private static int sum = 0;
    private static CountDownLatch countDownLatch = new CountDownLatch(4);
    public static void main(String[] args) throws InterruptedException {
   
        ThreadTest threadTest = new ThreadTest();
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for(int i=1; i<=76; i+=25){
   
            pool.execute(threadTest.new ThreadSum(i, i+24));
        }
        countDownLatch.await();
        pool.shutdown();

        System.out.println("totalSum: " + sum);
    }
    class ThreadSum extends Thread{
   
        private int begin;
        private int end;
        public ThreadSum(int begin, int end){
   
            this.begin = begin;
            this.end = end;
        }

        @Override
        public void run() {
   
            try {
   
                Thread.sleep(100);//便于测试
            } catch (InterruptedException e) {
   
                e
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值