栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程编程题汇总

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程编程题汇总

一、两个线程奇偶顺序打印轮流打印1-100数字 1.1 实现方式1-----对共享变量加synchronized锁

【代码】

package com.alipay.insttrade.common.service.integration.payment;


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
package com.alipay.insttrade.common.service.integration.payment;


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();
                        }
                    }

                }
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678850.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号