Sunday, September 8, 2013

Why this piece of JAVA code is not running concurrent

Why this piece of JAVA code is not running concurrent

I have written Sieve of Eratosthenes which supposed to work parallel, but
it's not. When I increase number of threads time of computing is not
getting lower. Any ideas why?
Main class
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConcurrentTest {
public static void main(String[] args) throws InterruptedException {
Sieve task = new Sieve();
int x = 1000000;
int threads = 4;
task.setArray(x);
Long beg = new Date().getTime();
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < threads; i++) {
exec.execute(task);
}
exec.shutdown();
Long time = 0L;
//Main thread is waiting until all threads are terminated( it means that
computing is done)
while (true)
if (exec.isTerminated()) {
time = new Date().getTime() - beg;
break;
}
// task.printArray();
System.out.println("Time is " + time);
}
}
Sieve class
import java.util.concurrent.ConcurrentHashMap;
public class Sieve implements Runnable {
private ConcurrentHashMap<Integer, Boolean> array = new
ConcurrentHashMap<Integer, Boolean>();
private int x;
public void run() {
while(true){
//I am getting synchronized number to check if it's prime
int n = getCounter();
//If no more numbers to check, stop loop
if( n == -1)
break;
//If HashMap contains number, we can further
if(!array.containsKey(n))continue;
for (int i = 2 * n; i <= x; i += n) {
//Complex numbers are removed from HashMap so there are no double
verifications of numbers. Eg. 6, 12 and much more.
array.remove(i);
}
}
}
private synchronized int getCounter(){
if( counter < x)
return counter++;
else return -1;
}
public void setArray(int x) {
this.x = x;
for (int i = 2; i <= x; i++)
array.put(i, false);
}
public void printArray() {
for (int i = 2; i <= x; i++) {
if(array.containsKey(i))
if (!array.get(i))
System.out.println(i);
}
}
}
I made some tests with different number of threads. These are results:
Nr of threads 1
Time is 1850
Time is 1795
Time is 1825
Nr of threads 2
Time is 1845
Time is 1836
Time is 1814
Nr of threads 3
Time is 1767
Time is 1820
Time is 1756
Nr of threads 4
Time is 1732
Time is 1840
Time is 2083
Nr of threads 5
Time is 1791
Time is 1795
Time is 1803
Nr of threads 6
Time is 1825
Time is 1728
Time is 1707
Nr of threads 7
Time is 1754
Time is 1729
Time is 1686
Nr of threads 8
Time is 1760
Time is 1717
Time is 1817
Nr of threads 9
Time is 1721
Time is 1699
Time is 1673
Nr of threads 10
Time is 1661
Time is 1722
Time is 1718

No comments:

Post a Comment