zoukankan      html  css  js  c++  java
  • 读Java并发编程实践中,向已有线程安全类添加功能--客户端加锁实现示例

    在Java并发编程实践中4.4中提到向客户端加锁的方法。此为验证示例,写的不好,但可以看出结果来。

    package com.blackbread.test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class GoodListHelper<E> {
        public List<E> list = Collections.synchronizedList(new ArrayList<E>());
    
        public boolean putIfAbsent(E x) throws InterruptedException {
            synchronized (list) {
                boolean absent = !list.contains(x);
                if (absent) {
                    if (list.contains(x))
                        System.out.println(list.contains(x));
                    list.add(x);
                }
                return absent;
            }
        }
    
        public class PrintA extends Thread {
            private GoodListHelper<String> goodListHelper;
            private String value;
    
            public PrintA(GoodListHelper<String> goodListHelper, String value) {
                this.goodListHelper = goodListHelper;
                this.value = value;
            }
    
            @Override
            public void run() {
                try {
                    goodListHelper.putIfAbsent(value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public class PrintB extends Thread {
            private GoodListHelper<String> goodListHelper;
            private String value;
    
            public PrintB(GoodListHelper<String> goodListHelper, String value) {
                this.goodListHelper = goodListHelper;
                this.value = value;
            }
    
            @Override
            public void run() {
                goodListHelper.list.add(value);
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            final GoodListHelper<String> goodListHelper = new GoodListHelper<String>();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ExecutorService executor = Executors.newFixedThreadPool(50);
                    for (int i = 0; i < 1000; i++) {
                        Thread t = goodListHelper.new PrintA(goodListHelper,
                                String.valueOf(i));
                        executor.execute(t);
                    }
                    executor.shutdown();
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ExecutorService executor = Executors.newFixedThreadPool(50);
                    for (int i = 0; i < 1000; i++) {
                        Thread t = goodListHelper.new PrintB(goodListHelper,
                                String.valueOf(i));
                        executor.execute(t);
                    }
                    executor.shutdown();
                }
            }).start();
        }
    }
  • 相关阅读:
    Aruba无线控制器添加黑名单
    H3C POE交换机办公网常用配置
    H3C接入交换机办公网常用配置
    H3C核心交换机办公网常用配置
    H3C IRF-原理
    华为交换机SSH远程配置
    H3C无线AC黑白名单配置
    H3C 5130交换机初始化配置
    H3C交换机恢复出厂设置
    H3C交换机SSH登录配置
  • 原文地址:https://www.cnblogs.com/lcxdever/p/3958544.html
Copyright © 2011-2022 走看看