import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
public class Main implements Runnable
{
static String[] entries;
static int count;
static HashMap<String,String> finalMap;
public static void main(String[] args) throws IOException, InterruptedException {
count = 0;
File f = new File("rd.txt");
String contents = Files.toString(f, Charsets.UTF_8);
entries = contents.split("\n");
finalMap = new HashMap<String,String>(entries.length);
ArrayList<Thread> threads = new ArrayList();
for(int i=0; i<10; i++)
{
Runnable temp = new Main();
Thread t = new Thread(temp);
t.setName("thread"+i);
t.start();
threads.add(t);
}
for(int i=0; i<10; i++)
{
threads.get(i).join();
}
System.out.println(finalMap);
System.out.println(finalMap.entrySet().size());
Set<String> set1 = finalMap.keySet();
System.out.println(set1);
System.out.println(set1.size());
}
@Override
public void run() {
while(true)
{
String temp ;
System.out.println("Thread active "+Thread.activeCount());
System.out.println("Count by thread: "+count + Thread.currentThread().getName());
if(count <entries.length)
{
synchronized(this)
{
temp = entries[count];
count++;
}
System.out.println(temp);
String info[] = temp.split("[|]");
synchronized (this) {
finalMap.put(info[0], info[1]+"written by "+Thread.currentThread().getName());
}
}
else
{
break;
}
}
}
}
There are entries in the rd.txt which follow the following formatting
k1|v1,v2
k2|v3,v4
k3|v5,v6
.
.
.
I have used Google guava library for reading the text file ( just FYI ). So the aim of the program is to read a huge data set using multithreading. but when i run the code i run into arrayoutofboundexception though i have used synchronization on count variable so it must never cross the limit right? so any help regarding this will be helpful.
Also the number of entries read fluctuate on every run..