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();
        }
    }
  • 相关阅读:
    编程题目----约德尔测试
    编程题目----路灯
    java----java垃圾回收算法
    JAVA多线程和并发基础面试问答(转载)
    科学史上非常伟大的十位单身科学家
    MySQL----mysql57服务突然不见了的,解决方法
    linux:644、755、777权限详解
    Java 关键字 速查表
    java----鲁棒性
    习题----第六章 图(转)
  • 原文地址:https://www.cnblogs.com/lcxdever/p/3958544.html
Copyright © 2011-2022 走看看