I\'m trying to write a multithreaded web crawler.
My main entry class has the following code:
ExecutorService exec = Executors.newFixedThreadPool(num
I'd like to suggest an AdaptiveExecuter. Based on a characteristic value, you can choose to serialize or parallalize a thread for execution. In the sample below, PUID is a string/object that I wanted to use to make that decision. You can alter the logic to suit your code. Some portions of code are commented to allow further experiments.
class AdaptiveExecutor implements Executor { final Queue tasks = new LinkedBlockingQueue(); Runnable active ; //ExecutorService threadExecutor=Executors.newCachedThreadPool(); static ExecutorService threadExecutor=Executors.newFixedThreadPool(4);
AdaptiveExecutor() {
System.out.println("Initial Queue Size=" + tasks.size());
}
public void execute(final Runnable r) {
/* if immediate start is needed do either of below two
new Thread(r).start();
try {
threadExecutor.execute(r);
} catch(RejectedExecutionException rEE ) {
System.out.println("Thread Rejected " + new Thread(r).getName());
}
*/
tasks.offer(r); // otherwise, queue them up
scheduleNext(new Thread(r)); // and kick next thread either serial or parallel.
/*
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
*/
if ((active == null)&& !tasks.isEmpty()) {
active = tasks.poll();
try {
threadExecutor.submit(active);
} catch (RejectedExecutionException rEE) {
System.out.println("Thread Rejected " + new Thread(r).getName());
}
}
/*
if ((active == null)&& !tasks.isEmpty()) {
scheduleNext();
} else tasks.offer(r);
*/
//tasks.offer(r);
//System.out.println("Queue Size=" + tasks.size());
}
private void serialize(Thread th) {
try {
Thread activeThread = new Thread(active);
th.wait(200);
threadExecutor.submit(th);
} catch (InterruptedException iEx) {
}
/*
active=tasks.poll();
System.out.println("active thread is " + active.toString() );
threadExecutor.execute(active);
*/
}
private void parallalize() {
if(null!=active)
threadExecutor.submit(active);
}
protected void scheduleNext(Thread r) {
//System.out.println("scheduleNext called") ;
if(false==compareKeys(r,new Thread(active)))
parallalize();
else serialize(r);
}
private boolean compareKeys(Thread r, Thread active) {
// TODO: obtain names of threads. If they contain same PUID, serialize them.
if(null==active)
return true; // first thread should be serialized
else return false; //rest all go parallel, unless logic controlls it
}
}