Created
February 14, 2014 04:13
-
-
Save rsrini7/b12ec5845886915e23fd to your computer and use it in GitHub Desktop.
ArrayList, Collections.synchronizedList and CopyOnWriteArrayList with null simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.srini.util; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.concurrent.CopyOnWriteArrayList; | |
| public class NullSimulator implements Runnable | |
| { | |
| public static Map<String,List<String>> map=new HashMap<String, List<String>>(); | |
| public static void main(String[] args) throws InterruptedException | |
| { | |
| List<String> list= new CopyOnWriteArrayList<String>();//Collections.synchronizedList(new ArrayList<String>());new ArrayList<String>(); | |
| for(int i=0;i<50000;i++) | |
| { | |
| list.add(String.valueOf(i)); | |
| } | |
| map.put("val", list); | |
| for (int i=0;i<2;i++){ | |
| new Thread(new NullSimulator(),String.valueOf(i)).start(); | |
| } | |
| } | |
| @Override | |
| public void run() | |
| { | |
| List<String> lst=map.get("val"); | |
| if(lst!=null&lst.size()>0){ | |
| for(int i=0;i<lst.size();i++){ | |
| lst.add(lst.get(i)); | |
| lst.remove(i); | |
| map.put("val", lst); | |
| if(map.get("val").contains(null)) | |
| { | |
| System.out.println((i+1)+"- "+(Integer.parseInt(Thread.currentThread().getName())+1)); | |
| return; | |
| // throw new NullPointerException(); | |
| } | |
| //else System.out.println("no null"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment