Java Multithreading issue

I need to write a multi threaded version of the Sieve of Eratosthenes. I think I need to use a binary Semaphone for knowing when to kick off a new thread, but I also cant use busy waiting so I'm not sure.

I've written about 20 versions of this code and I cna't seem to get it to work. Here's what I have right now

import java.util.ArrayList;

import java.util.concurrent.LinkedBlockingQueue;

publicclass testFilterimplements Runnable

{

privatevolatilestatic ArrayList<Integer> al =new ArrayList<Integer>();

privatevolatilestatic ArrayList<Integer> primes =new ArrayList<Integer>();

privatevolatilestatic LinkedBlockingQueue<Integer> lbq =new LinkedBlockingQueue<Integer>();

publicstaticvoid main(String[] args)throws Exception

{

init( 2000 );

sendThread();

Thread.sleep( 1000 );

print();

}

publicstaticsynchronizedvoid sendThread()

{

if ( !al.isEmpty() )

{

testFilter tf =new testFilter();

Thread thread =new Thread( tf );

int newPrime = al.remove( 0 );

lbq.add( newPrime );

primes.add( newPrime );

thread.start();

}

}

publicstaticvoid init(int size)

{

for (int i = 2; i <= size; i++ )

{

al.add( i );

}

}

publicstaticsynchronizedvoid print()

{

for (int i = 0; i < primes.size(); i++ )

{

System.out.println( primes.get( i ) );

}

}

publicvoid run()

{

boolean threadSent =false;

int remove = lbq.poll();

for (int i = 0; i < al.size(); i++ )

{

int current = al.get( i );

if ( current % remove == 0 )

{

if ( !threadSent )

{

al.remove( i );

threadSent =true;

}

sendThread();

}

}

}

}

Now this isn't working and I can't seem to figure out why. What I think I want to do it kick off a new thread as soon as the current thread has made a change to the List. This means that the first element in the list is a prime....I'm sure I'm missing something basic, any help would be greatly appreciated.

Message was edited by:

jstudent1986

[4668 byte] By [jstudent1986a] at [2007-10-3 9:09:14]
# 1

You do realise this is going to be substantially slower than a single-threaded program, don't you? The point of threading tends to be that when one thread is waiting other threads can use the CPU time. With all threads solidly using the CPU there will be no performance benefit, and a lot of overheads. Especially if you start huge number of threads.

And you don't use blocking queues if you have a thread for everything.

And the seive uses a boolean array, not an array of integers (BitSet might be suitable).

If you want your program to wait for a whole bunch of threads before printing, put the Threads in a List, then at the end call join on each one of them.

BlockingQueue.poll is apt to return null if there are no entries available. You probably meant take.

volatile isn't appropriate, the contents of the collections might be but the reference to the collections isn't. You might need to use Synchronized versions (use Collections methods) of the Collections (this will make your program even slower).

And why, when doing the crossing out bit, don't you simply increment the index by the prime you are processing, rather than increment it by 1 and check divisibility?

malcolmmca at 2007-7-15 4:21:03 > top of Java-index,Java Essentials,Java Programming...