I'm new to concurrent programing and have been working on code which has a queue of items to be processed, this is passed to some worker threads, the number specified by the user. At the moment I've just tried to do it with two worker threads plus the main.
private static class workerThread extends Thread {
workerThread(){
super();
}
public void run(){
while (!workQueue.isEmpty()) {
String x = workQueue.remove();
//System.out.println("work queue size: " + workQueue.size());
Vector<String> list2 = new Vector<String>((Vector) table.get(x));
list2 = process(x, list2);
//System.out.println(list2 + "list2");
table.put(x, list2);
//System.out.println(x + "key" + "value" + vvv);
}
}
That's the thread workerthread class, I've tried to call it just by creating two new threads:
workerThread wt = new workerThread();
workerThread wt2 = new workerThread();
wt.start();
wt2.start();
try {
wt.join();
wt2.join();
} catch (InterruptedException ex) {
Logger.getLogger(includeCrawler.class.getName()).log(Level.SEVERE, null, ex);
}
I'm not sure if this is right, or will have any benfit due to waiting for the joins? Thanks for any help.
A much cleaner and scalable way to do this is to use a thread pool created by the Executors class.
By the way, the Vector
class is obsolete and should not be used anymore - use ArrayList
instead and dump whatever book or tutorial where you learned to use Vector
- it's more than a decade out of date.