线程同步

线程同步

多个线程操作同一个资源

不安全的购票

// 不安全的买票
public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket, "李宇杰").start();
        new Thread(buyTicket, "郭灿灿").start();
        new Thread(buyTicket, "Richard").start();
    }



}
class BuyTicket implements Runnable {
    private int ticketNums = 10;
    boolean flag = true; // 外部停止
    @Override
    public void run() {
        while (flag) {
            buy();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void buy() {
        if (ticketNums <= 0) {
            flag = false;
            return;
        }
        System.out.println(Thread.currentThread().getName() + "买了第" + ticketNums-- + "张票.");

    }
}

不安全的取钱

// 不安全的取钱
public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(100, "结婚基金");

        Drawing li = new Drawing(account,50,"李宇杰");
        Drawing guo = new Drawing(account,100,"郭灿灿");
        guo.start();
        li.start();

    }
}

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

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

// 银行:模拟取款
class Drawing extends Thread{
    Account account; // 账户
    // 取多少
    int drawingMonry;
    // 现在手里有多少钱
    int nowMoney;

    public Drawing(Account account, int drawingMonry, String name) {
        super(name);
        this.account = account;
        this.drawingMonry = drawingMonry;

    }

    @Override
    public void run() {
        // 判断有没有钱
        if (account.money - drawingMonry < 0) {
            System.out.println(Thread.currentThread().getName() + "钱不够,取不了");
            return;
        }
        try {// 可以放大问题的发生
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 卡内余额
        account.money = account.money - drawingMonry;
        // 你手里的钱
        nowMoney = nowMoney + drawingMonry;
        System.out.println(account.name + "余额为:" + account.money + "万元");
        //Thread.currentThread().getName() == this.getName()
        System.out.println(this.getName() + "手里有:" + nowMoney + "万元");
    }
}

不安全的集合List 两个线程同一时间操作的统一位置 造成了覆盖。

// 线程不安全的集合
public class UnsafeList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                list.add(Thread.currentThread().getName());
            }).start();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}

一定要注意锁的对象 是需要增删改查的

买票改进

// 安全的买票
public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket, "李宇杰").start();
        new Thread(buyTicket, "郭灿灿").start();
        new Thread(buyTicket, "Richard").start();
    }



}
class BuyTicket implements Runnable {
    private int ticketNums = 10;
    boolean flag = true; // 外部停止
    @Override
    public void run() {
        while (flag) {
            try {
                Thread.sleep(100);
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    //synchronized 同步方法, 锁的是this
    private synchronized void buy() throws InterruptedException {
        if (ticketNums <= 0) {
            flag = false;
            return;
        }

        System.out.println(Thread.currentThread().getName() + "买了第" + ticketNums-- + "张票.");

    }
}

取钱改进

// 安全的取钱
public class UnsafeBank {
    public static void main(String[] args) {
        Account account = new Account(1000, "结婚基金");

        Drawing li = new Drawing(account,50,"李宇杰");
        Drawing guo = new Drawing(account,100,"郭灿灿");

        guo.start();
        li.start();

    }
}

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

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

// 银行:模拟取款
class Drawing extends Thread{
    Account account; // 账户
    // 取多少
    int drawingMonry;
    // 现在手里有多少钱
    int nowMoney;

    public Drawing(Account account, int drawingMonry, String name) {
        super(name);
        this.account = account;
        this.drawingMonry = drawingMonry;

    }

    @Override
    public  void run() {

        // 锁的对象一定是变化的量   需要增删改
        synchronized(account) {
            // 判断有没有钱
            if (account.money - drawingMonry < 0) {
                System.out.println(Thread.currentThread().getName() + "钱不够,取不了");
                return;
            }
            try {// 可以放大问题的发生
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 卡内余额
            account.money = account.money - drawingMonry;
            // 你手里的钱
            nowMoney = nowMoney + drawingMonry;
            System.out.println(account.name + "余额为:" + account.money + "万元");
            //Thread.currentThread().getName() == this.getName()
            System.out.println(this.getName() + "手里有:" + nowMoney + "万元");
        }

    }
}

list集合改进

// 线程安全的集合
public class UnsafeList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

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

JUC 已经安全的 锁

需要导入import java.util.concurrent.CopyOnWriteArrayList;

// 测试JUC安全类型的集合
public class TestJUC {
    public static void main(String[] args) {
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                list.add(Thread.currentThread().getName());
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页