I'm researching how to do Multithreading, mainly from this website: http://www.tutorialspoint.com/java/java_multithreading.htm
I'm trying to implement it into my own Hash Map Implementation. My thought is, when I re-size my hashmap I can multi thread so that while it adds in the entry to the larger hash map, I can use the other thread to re-hash the smaller hash map into the larger one.
My implementation of the Hash Map works correctly - however I'm struggling to get to grips with the Multithreading, so I was wondering if anyone could take a look at my code and see where I'm going wrong?
public class HashMap extends Thread
{
private Thread t;
private String threadName;
private long noofitems;
private HashPair[] data;
HashPair[] newArray;
private int copyCounter = 0;
public HashMap(int initlen)
{
noofitems=0;
data=new HashPair[initlen];
threadName = "t1";
}
public void run()
{
for (int i = 0; i < 5 && copyCounter < noofitems; i++)
{
if (data[copyCounter] != null)
{
int test1=HashFunction(data[copyCounter].key);
// newArray[test1] = data[copyCounter];
int index = test1 % newArray.length;
boolean tempInserted = false;
int tempIncrement = 1;
while (!tempInserted)
{
if (newArray[index] == null)
{
newArray[index] = data[copyCounter];
noofitems++;
System.out.println("Thread Added");
tempInserted = true;
}
}
copyCounter++;
}
else
{
copyCounter++;
}
}
}
//make data point to newArray
//null newArray
//if copyCounter >= data.length { do null thing}
public void AddItem(String key, String value)
{
// System.out.println("Adding: "+key+" "+value);
int index=HashFunction(key);
//++hits[index%data.length];
HashPair item=new HashPair(key, value);
// Task 3: Check load factor here and resize if over 0.7
if ((noofitems/(float)data.length) > 0.7 && newArray == null)
{
newArray = new HashPair[data.length*2];
//copyCounter = 0;
}
// Task 2 Code: Insert item into the data, but check and resolve collisions first
// When you have this, implement the GetValue method
if (newArray == null)
{
index = index % data.length;
boolean inserted = false;
int increment = 1;
while (!inserted)
{
if (data[index] == null)
{
data[index] = item;
noofitems++;
inserted = true;
}
}
}
else
{
if (t == null)
{
t = new Thread(this, threadName);
t.start();
}
index = index % newArray.length;
boolean inserted = false;
int increment = 1;
while (!inserted)
{
if (index < 0)
System.out.println();
if (newArray[index] == null)
{
newArray[index] = item;
noofitems++;
inserted = true;
}
}
}
}
private int HashFunction(String key)
{
// Task 1 code: Hash the key and return a long value
int code = 38;
for (int i=0; i < key.length(); i++)
{
code = code*3+(key.charAt(i));
}
return (code>0?code:-code);
}
At the moment I've set it to copy 5 of the small hash maps entries into the larger hash map at a time - however thinking about it, it'll probably be better to copy one at a time? Otherwise the main thread will idle while the 2nd thread finishes copying the remaining 4 across?
I currently get this error when trying to execute my program:
Thread Added
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1583
at hashmaps.HashMap.AddItem(HashMap.java:119)
at hashmaps.Dictionary.CreateDictionary(Dictionary.java:71)
at hashmaps.Dictionary.main(Dictionary.java:15)
Java Result: 1
Line 119 = if (newArray[index] == null)
index2 = index2 + (increment2<<1);?HashMapextendThread?`Is it a real thread? I don't think so.