首页 > 编程语言 > 详细

多线程礼让+三个不安全案例

时间:2021-05-04 17:53:34      阅读:21      评论:0      收藏:0      [点我收藏+]

13.多线程的yield()礼让方法,不一定会成功,根据cpu的调度

package com.wen.thread;

/**
 * @Author WangEn
 * @CreateTime: 2021-04-12-11-10
 * @Emile: wen_5988@163.com
 */
// 测试多线程礼让  yield()方法   当时不一定会执行,还是根据cpu的调用
public class TestYield implements Runnable {

    public static void main(String[] args) {
        TestYield testYield = new TestYield();
        new Thread(testYield,"A").start();
        Thread.yield();
        new Thread(testYield,"B").start();
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"关闭---》");
        System.out.println(Thread.currentThread().getName()+"打开---》");
    }
}

14.抢票加锁案例

package com.wen.thread;

/**
 * @Author WangEn
 * @CreateTime: 2021-04-16-20-52
 * @Emile: wen_5988@163.com
 */
public class TestTick_One {

    public static void main(String[] args) {
        Buy buy = new Buy();
        new Thread(buy,"小明").start();
        new Thread(buy,"黄牛党").start();
        new Thread(buy,"二明").start();
    }
}

class Buy implements Runnable {
    private int tickNums = 100;
    boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            try {
                BuyTIck();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void BuyTIck() throws InterruptedException {// 同步监视器的对象是修改的变量
        if (tickNums<=0){
            flag = false;
            return;
        }
        Thread.sleep(100);
        System.out.println(Thread.currentThread().getName() + "-->抢到了第" + tickNums-- + "张票");
    }
}



15.结婚基金加锁案例(要在修改上面加锁,在方法内部加锁)锁的是变化的量(增删改)

package com.wen.thread;

/**
 * @Author WangEn
 * @CreateTime: 2021-04-16-21-07
 * @Emile: wen_5988@163.com
 */
public class TestJieHunJiJin {

    public static void main(String[] args) {
        Account account = new Account("结婚基金", 100);
        new JieHun(account,50,"丈夫").start();
        new JieHun(account,100,"妻子").start();
    }
}

// 定义账户
class Account {
    public String name;
    public int money;

    public Account(String name, int money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

// 定义人
class JieHun extends Thread {
    private Account account;
    private int drawingMoney;
    private int nowMoney;

    public JieHun(Account account, int drawingMoney, String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;
    }

    @Override
    public void run() {
        synchronized (account){// 同步监视器的对象是修改的变量
            if (account.money - drawingMoney < 0) {
                System.out.println("钱不够!!!");
                return;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.money -= drawingMoney;
            nowMoney += drawingMoney;
            System.out.println(account.name + "余额为" + account.money);
            System.out.println(this.getName() + "手里面的钱为" + nowMoney);
        }

    }
}

16.集合安全案例ArrayList

package com.wen.线程不安全的集合;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author WangEn
 * @CreateTime: 2021-05-04-15-05
 * @Emile: wen_5988@163.com
 */
public class UnsafeList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (list){// 同步监视器的对象是修改的变量
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }

}

多线程礼让+三个不安全案例

原文:https://www.cnblogs.com/WangEn/p/14729604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!