JavaSE-多线程(2.1)- 锁举例1(synchronized)

本文详细介绍了如何在Java SE中使用`synchronized`关键字确保在SynchronizedTest4类的put和get方法中,多线程间的操作不会导致数据不一致。通过实例演示了线程安全的容器操作,包括并发put和get的互斥控制。

JavaSE-多线程(2.1)- 锁举例1(synchronized)

package com.hs.example.base.multithread.day01;

import java.util.LinkedList;

/**
 * 自定义容器,固定容量最大值为MAX_COUNT,运行多线程并发put和get
 * @param <T>
 */
public class SynchronizedTest4<T> {

    final LinkedList<T> lists = new LinkedList<>();
    final int MAX_COUNT = 10;
    private int count = 0;

    /**
     * 加锁,防止count与实际lists大小不一致
     *
     * @param t
     */
    public synchronized void put(T t) {
        System.out.println(Thread.currentThread().getName() + " put start...");
        /**
         * 这里为什么用 while 不用if,
         * 比如当前wait线程是a,线程c执行get方法后唤醒a线程,此时 lists.size() = MAX_COUNT -1 
         * a需要重新获取锁,此时线程b也执行put方法,也需要获取锁
         * 如果线程b获取锁成功,那么 lists.size() = MAX_COUNT 
         * 此时a获取锁成功,继续执行,如果不判断 lists.size() == MAX_COUNT
         * 会导致 lists.size() 超出范围
         */
        while (lists.size() == MAX_COUNT) {
            try {
                System.out.println(Thread.currentThread().getName() + " waiting...");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        lists.add(t);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count++;
        this.notifyAll();
        System.out.println(Thread.currentThread().getName() + " put end...");
    }

    public synchronized T get() {
        System.out.println(Thread.currentThread().getName() + " get start...");
        T t = null;
        while (lists.size() == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        t = lists.removeFirst();
        count--;
        this.notifyAll();
        System.out.println(Thread.currentThread().getName() + " get end...");
        return t;
    }

    public static void main(String[] args) {
        SynchronizedTest4 synchronizedTest4 = new SynchronizedTest4();
        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(() -> {
                synchronizedTest4.put("str");
            }, "put thread" + i);
            t.start();
        }

        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(() -> {
                synchronizedTest4.get();
            }, "get thread" + i);
            t.start();
        }
    }
}

put thread0 put start...
put thread0 put end...
get thread5 get start...
get thread5 get end...
get thread18 get start...
get thread19 get start...
get thread15 get start...
get thread11 get start...
get thread17 get start...
get thread10 get start...
get thread16 get start...
get thread13 get start...
get thread12 get start...
get thread9 get start...
get thread8 get start...
get thread4 get start...
get thread14 get start...
get thread7 get start...
get thread6 get start...
get thread0 get start...
get thread3 get start...
get thread1 get start...
get thread2 get start...
put thread19 put start...
put thread19 put end...
put thread18 put start...
put thread18 put end...
put thread15 put start...
put thread15 put end...
put thread14 put start...
put thread14 put end...
put thread9 put start...
put thread9 put end...
put thread16 put start...
put thread16 put end...
put thread13 put start...
put thread13 put end...
put thread12 put start...
put thread12 put end...
put thread11 put start...
put thread11 put end...
put thread10 put start...
put thread10 put end...
put thread17 put start...
put thread17 waiting...
put thread7 put start...
put thread7 waiting...
put thread6 put start...
put thread6 waiting...
put thread8 put start...
put thread8 waiting...
put thread3 put start...
put thread3 waiting...
put thread5 put start...
put thread5 waiting...
put thread4 put start...
put thread4 waiting...
put thread2 put start...
put thread2 waiting...
put thread1 put start...
put thread1 waiting...
get thread2 get end...
get thread1 get end...
get thread3 get end...
get thread0 get end...
get thread6 get end...
get thread7 get end...
get thread14 get end...
get thread4 get end...
get thread8 get end...
get thread9 get end...
put thread1 put end...
put thread2 put end...
put thread4 put end...
put thread5 put end...
put thread3 put end...
put thread8 put end...
put thread6 put end...
put thread7 put end...
put thread17 put end...
get thread18 get end...
get thread19 get end...
get thread15 get end...
get thread11 get end...
get thread17 get end...
get thread10 get end...
get thread16 get end...
get thread13 get end...
get thread12 get end...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值