线程同步(重点)

2021-03-03 07:28

阅读:619

标签: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 拿到了第"+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 

技术图片

package com.hao.syn;


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

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

    public static void main(String[] args) {
        List list = new ArrayList();
        for (int i = 0; i {
                //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


评论


亲,登录后才可以留言!