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();
        }
    }
  • 相关阅读:
    与(&,&&)和或(|,||)的区别
    vue笔记(更新中)
    echarts实现心脏图的滚动三种实现方法
    生成四则运算
    软件工程第四次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    前端优化
    关于事件监听
  • 原文地址:https://www.cnblogs.com/lcxdever/p/3958544.html
Copyright © 2011-2022 走看看