Source Code : Use ConcurrentHashMap
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Use ConcurrentHashMap
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class Test {
static Set<Thread> updateThreads = new HashSet<Thread>();
public static void main(String[] args) {
ConcurrentMap<Integer, String> concurrentMap = new ConcurrentHashMap<Integer, String>();
for (int i = 0; i < 1000; i++) {
startUpdateThread(i, concurrentMap);
}
for (Map.Entry<Integer, String> entry : concurrentMap.entrySet()) {
System.out.println("Key :" + entry.getKey() + " Value:" + entry.getValue());
}
for (Thread thread : updateThreads) {
thread.interrupt();
}
}
private static void startUpdateThread(int i,
final ConcurrentMap<Integer, String> concurrentMap) {
Thread thread = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
Random random = new Random();
int randomInt = random.nextInt(20);
concurrentMap.put(randomInt, UUID.randomUUID().toString());
}
}
});
thread.setName("Update Thread " + i);
updateThreads.add(thread);
thread.start();
}
}
Thank with us