码迷,mamicode.com
首页 > 编程语言 > 详细

线程同步(重点)

时间:2021-02-15 12:33:30      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:http   new   alt   with   操作   over   ++   资源   nts   

来源:https://www.bilibili.com/video/BV1V4411p7EF?p=20
感谢老师!
技术图片
技术图片

package com.hao.syn;
//三个线程操作同一个资源
//线程不安全,出现负数。。。sleep放大不安全情况
public class UnsafeBuyTickets {
    public static void main(String[] args) {
        BuyTickets buyTickets = new BuyTickets();

        new Thread(buyTickets,"苦逼的我-----").start();
        new Thread(buyTickets,"牛逼的你-----").start();
        new Thread(buyTickets,"黄牛党-----").start();
    }
}

class BuyTickets implements Runnable{
    //票
    private int TicketNums = 10;
    boolean flag = true;//外部停止方式

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

    //synchronized 同步方法
    //买票
    public synchronized void buy() throws InterruptedException {
        //判断是否有票
        if (TicketNums <= 0){
            flag = false;
            return;
        }
        //模拟延时
        Thread.sleep(100);
        //买票
        System.out.println(Thread.currentThread().getName()+"------->拿到了第"+TicketNums--+"张票");
    }
}

技术图片

package com.hao.syn;

//俩人同时银行取钱
public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(50,"结婚基金");

        WithDrawMoney you = new WithDrawMoney(account,50,"你");
        WithDrawMoney wife = new WithDrawMoney(account,100,"你wife");
        you.start();
        wife.start();
    }
}

//账户
class Account{
    int money;//余额
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}
//银行:模拟取款
class WithDrawMoney extends Thread{
    Account account;//账户
    int withdrawMoney ;//取了多少钱
    int nowMoney;//还剩多少钱

    public WithDrawMoney(Account account,int withdrawMoney,String name){
        super(name);//线程名字
        this.account = account;
        this.withdrawMoney = withdrawMoney;

    }

    //取钱
    @Override
    public void run() {

      //synchronized 默认锁的是this,同步块
        synchronized (account) {
            //判断有没有钱
            if (account.money - withdrawMoney < 0) {
                System.out.println(Thread.currentThread().getName() + "取的时候,提示钱不够,取不了");
                return;
            }
            //sleep
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            account.money -= withdrawMoney;
            nowMoney += withdrawMoney;

            System.out.println(account.name + "余额为:" + account.money);
            System.out.println(Thread.currentThread().getName() + "手里有" + nowMoney);
        }
    }
}

技术图片

package com.hao.syn;


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

//线程不安全集合
public class UnsafeList {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                //synchronized,锁的对象是增删改的对象
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}

技术图片

线程同步(重点)

标签:http   new   alt   with   操作   over   ++   资源   nts   

原文地址:https://www.cnblogs.com/haohaoxuexio/p/14397991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!