Skip to content

Instantly share code, notes, and snippets.

@rsrini7
Created February 14, 2014 04:13
Show Gist options
  • Select an option

  • Save rsrini7/b12ec5845886915e23fd to your computer and use it in GitHub Desktop.

Select an option

Save rsrini7/b12ec5845886915e23fd to your computer and use it in GitHub Desktop.
ArrayList, Collections.synchronizedList and CopyOnWriteArrayList with null simulator
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