多线程编程题汇总

一、两个线程奇偶顺序打印轮流打印1-100数字

1.1 实现方式1-----对共享变量加synchronized锁

【代码】

/*
 * Ant Group
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.alipay.insttrade.common.service.integration.payment;

/**
 * @author zhannan
 * @version Test.java, v 0.1 2021年12月23日 19:59 zhannan
 */
public class Test{

    public static int count = 1;
    public static final Object lock = new Object();

    public static void main(String[] args) {


        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(count<=100){
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + "抢到锁");
                        //算是一种double check
                        if (count <= 100 && count%2==1) {
                            System.out.println(Thread.currentThread().getName() + ":" + count);
                            count++;
                        }
                    }
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(count<=100){
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + "抢到锁");

                        if (count <= 100 && count%2==0) {
                            System.out.println(Thread.currentThread().getName() + ":" + count);
                            count++;
                        }
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}

【output】
在这里插入图片描述
【缺点】
可以看到虽然实现了题目。

但实际上并不代表着这两个线程在交替运行,因为线程是随机抢锁的,有可能连续十次都是偶数线程好运抢到了锁,只是因为不满足条件,抢到锁后发现不符合奇偶啥也不干,浪费资源

1.2 实现方式2-----线程间通信之wait/notify

/*
 * Ant Group
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.alipay.insttrade.common.service.integration.payment;

/**
 * @author zhannan
 * @version Test.java, v 0.1 2021年12月23日 19:59 zhannan
 */
public class Test{

    public static void main(String[] args) throws InterruptedException {

        new Thread(new TurningRunner(), "奇数").start();
        // 短暂休眠,确保线程执行顺序
        Thread.sleep(50);
        new Thread(new TurningRunner(), "偶数").start();

    }

    static class TurningRunner implements Runnable {
        private static int count = 1;
        private static final Object lock = new Object();

        @Override
        public void run() {
            while (count <= 100) {
                synchronized (lock) {

                    System.out.println(Thread.currentThread().getName() + ":" + count);
                    count ++;

                    //注意notify和wait顺序不能错
                    lock.notify(); // 唤醒另一个线程

                    if (count < 100) {
                        try {
                            lock.wait(); // 释放锁,进入阻塞状态,等待被唤醒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值