Thread advanced application-experience 8-JAVA5 application and case analysis of synchronous set Collections tool class in thread concurrent library

Source: Internet
Author: User

1. HashSet and HashMap of the relationship and differences.

Difference: The former is a single column, the latter is a double row, that is, HashMap has a key value, hashset only key;

Contact: HashSet's bottom is hashmap, can refer to the HashSet class source code, the default construction method is:

Public HashSet () {

Map = new Hashmap<key,object>

}

is hashset only use the HashMap key, but not his value, the former value can be programmer casually specified, anyway not

2. Thread Concurrent Library's collection Superior road inferior point

HashMap and HashSet If the cup fills up when thousands of threads are concurrent, the new technology Concurrent Library collection is developed in the JAVA5 thread concurrency library; the concurrentmap can be used. It is also possible to add a synchronous lock synchronized to the use of hashmap; in fact, Concurrentmap class is written in the original HashMap of all methods, but the return value is the original method plus a synchronous lock

3. Sync Collection

 4. Here is a thread concurrent library collection of the class case application, this case is closer to the knowledge point of the description package Com.java5.thread.newSkill;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Iterator;

Import java.util.concurrent.CopyOnWriteArrayList; public class Collectionmodifyexceptiontest {/** * @param args */public static void main (string[] args) {//cop Yonwritearraylist is a class in the thread concurrent library collection to avoid concurrent exceptions in HashMap/* Direct use of the traditional ArrayList will appear a variety of thread concurrency anomalies, interested can try the * * collection<u
		ser> users = new copyonwritearraylist<user> ();
		collection<user> users = new arraylist<user> ();
		Users.add (New User ("Yang", 21));
		Users.add (New User ("Yang Yi", 20));
		Users.add (New User ("Chic", 22));
		Iterator itrusers = Users.iterator ();
			while (Itrusers.hasnext ()) {User user = (User) itrusers.next ();
			if ("Chic". Equals (User.getname ()) {users.remove (user);
			else {System.out.println (user);

Helper Class: User Class Package Com.java5.thread.newSkill;
	public class User {private String name; Private int age;
		User (String name, int age) {super ();
		THIS.name = name;
	This.age = age;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
		@Override public int hashcode () {final int prime = 31;
		int result = 1;
		result = Prime * result + age;
		result = Prime * result + ((name = = null) 0:name.hashcode ());
	return result;
		@Override public boolean equals (Object obj) {if (this = obj) return true;
		if (obj = null) return false;
		if (GetClass ()!= Obj.getclass ()) return false;
		User other = (user) obj;
		if (age!= other.age) return false;
		if (name = = null) {if (other.name!= null) return false;
		else if (!name.equals (Other.name)) return false;
	return true;
	@Override public String toString () {return "User [age=" + Age + ", name=" + name + "]"; } @Override protected ObjectClone () throws Clonenotsupportedexception {return super.clone ();

 }
	
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.