zoukankan      html  css  js  c++  java
  • 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析

    1.  HashSet与HashMap的联系与区别?

       区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键;

       联系:HashSet的底层就是HashMap,可以参考HashSet的类源码,默认构造方法为:

       public HashSet(){

          map = new HashMap<key,Object>

       }

      就是HashSet只用HashMap的键,而不用他的值,前者的值可以程序员随便指定,反正不用

    2.  线程并发库中的集合优路劣之分

     HashMap和HashSet如果在成千上万个线程并发的时候会使cup占满;这时java5线程并发库中开发了新技术并发库集合;可以使用其中的ConcurrentMap,也可以在HashMap的使用时加上同步锁synchronized;其实ConcurrentMap类的编写中就是在重写原来HashMap中的所有方法,只是返回值是原来的方法加上了一个同步锁

    3.同步集合

      1  4.下面举一个线程并发库集合中的类的案例应用,这个案例比较贴近改知识点的说明
      2 package com.java5.thread.newSkill;
      3 
      4 import java.util.ArrayList;
      5 import java.util.Collection;
      6 import java.util.Iterator;
      7 import java.util.concurrent.CopyOnWriteArrayList;
      8 
      9 public class CollectionModifyExceptionTest {
     10 
     11     /**
     12      * @param args
     13      */
     14     public static void main(String[] args) {
     15 
     16         //CopyOnWriteArrayList为线程并发库集合中的类,可以避免HashMap中的并发异常
     17         /*
     18          * 直接使用传统的ArrayList会出现各式各样的线程并发异常异常,有兴趣的可以试试
     19          */
     20         Collection<User> users = new CopyOnWriteArrayList<User>();
     21         //Collection<User> users = new ArrayList<User>();
     22         users.add(new User("杨凯", 21));
     23         users.add(new User("杨旋", 20));
     24         users.add(new User("潇洒", 22));
     25         Iterator itrUsers = users.iterator();
     26         while (itrUsers.hasNext()) {
     27             User user = (User) itrUsers.next();
     28             if ("潇洒".equals(user.getName())) {
     29                 users.remove(user);
     30             } else {
     31                 System.out.println(user);
     32             }
     33         }
     34     }
     35 
     36 }
     37 
     38 辅助类:User类
     39 package com.java5.thread.newSkill;
     40 
     41 public class User {
     42 
     43     private String name;
     44     private int age;
     45     User(String name, int age) {
     46         super();
     47         this.name = name;
     48         this.age = age;
     49     }
     50     
     51     public String getName() {
     52         return name;
     53     }
     54 
     55     public void setName(String name) {
     56         this.name = name;
     57     }
     58 
     59     public int getAge() {
     60         return age;
     61     }
     62 
     63     public void setAge(int age) {
     64         this.age = age;
     65     }
     66 
     67     @Override
     68     public int hashCode() {
     69         final int prime = 31;
     70         int result = 1;
     71         result = prime * result + age;
     72         result = prime * result + ((name == null) ? 0 : name.hashCode());
     73         return result;
     74     }
     75     @Override
     76     public boolean equals(Object obj) {
     77         if (this == obj)
     78             return true;
     79         if (obj == null)
     80             return false;
     81         if (getClass() != obj.getClass())
     82             return false;
     83         User other = (User) obj;
     84         if (age != other.age)
     85             return false;
     86         if (name == null) {
     87             if (other.name != null)
     88                 return false;
     89         } else if (!name.equals(other.name))
     90             return false;
     91         return true;
     92     }
     93     @Override
     94     public String toString() {
     95         return "User [age=" + age + ", name=" + name + "]";
     96     }
     97     @Override
     98     protected Object clone() throws CloneNotSupportedException {
     99         return super.clone();
    100     }
    101     
    102 }
  • 相关阅读:
    JS数据类型
    javaws运行jnlp文件被阻止(安全级别过高)
    linux sysctl的使用
    面向对象-特性property
    面向对象-封装
    面向对象-鸭子类型
    面向对象-多态与多态性
    面向对象-抽象类
    面向对象-组合
    面向对象-在子类使用父类的对象或属性
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/6261904.html
Copyright © 2011-2022 走看看