Thread pools

I’m working with thread pools and wondering if anyone knows of such thread libraries or example code on the below examples is around. I don’t want to be reinventing the wheel.

A thread pool that will process 1 thread at a time and only a maximum of 1 thread waiting. So if something new comes in, it will remove the old runnable(and therefore never used) but not cancelling any runnables already running.

A thread pool that interrupts a runnable when running too long of X amount of seconds.

Your descriptions are not very clear (eg, “remove the old thread”?). You have seen this?
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html

[quote]You have seen this?
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html
[/quote]
That’s what I’m currently using. But they don’t offer by default (or not that I know of) of what I want.

[quote]Your descriptions are not very clear (eg, “remove the old thread”?).
[/quote]
Sorry I mean runnable.

Found a solution for the first one:


package manager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import util.LinkedBlockingStack;

public class ThreadPoolManager {
	private transient final static LinkedBlockingStack<Runnable> lbs = new LinkedBlockingStack<Runnable>();
	private transient final static ExecutorService threadPoolSmallTask = new ThreadPoolExecutor(3, 10, 1000, TimeUnit.MILLISECONDS,lbs );

	private ThreadPoolManager(){}

	public static void executeStackSmallTask(final Runnable r){
		ThreadPoolManager.threadPoolSmallTask.execute(r);
	}
	
	public static boolean containsRunnable(Runnable r){
		return lbs.contains(r);
	}
	public static void executeStackSmallTaskIfNotSame(final Runnable r){
		if(!containsRunnable(r)){
			executeStackSmallTask(r);
			System.out.println("Not same task");
		}else
			System.out.println("Same task");
	}
}

It’s very very simple

Try using Singletons and getInstance() menthods.



private static final class SingletonHolder {
        private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
}

public static ThreadPoolManager getInstance() {
        return SingletonHolder.INSTANCE;
}


This has to be the worst advice I’ve seen on here for a long time.

You have to be very explicit and clear when it comes to multithreading, and it’s best not to ask a question based on an vague idea you have of implementation, but rather explain your problem in words.

OUCH! ;D
This made me laugh so hard, I had to take a breather :stuck_out_tongue:

I’ve been toying with a very similar problem recently in my Praxis project. Praxis has a live compiler which allows the user to run fragments of live code in the video pipeline. Useful, and fun to play with, but I’d like to have a ‘sandboxed’ mode that doesn’t bring the video pipeline juddering to a halt if you pass in some dodgy code.

SO … best I’ve thought of so far would be to use submit() instead of execute() on the ExecutorService, and have a second monitoring thread that calls get(timeout…) on the returned Future. If it times out, you can then call cancel(true) on the Future to interrupt the task, but it does depend on the code handling interrupts gracefully. If you’ve got control of the tasks, that shouldn’t be a problem. If it’s code you can’t control (like me), handling things like hard loops and clearing up after a misbehaving Runnable, seem harder to implement. Thread.stop() anyone??? ;D

Hope that maybe helps you anyway.

hmm … but hidden in full view within this “advice”, which yes doesn’t touch the question … the idea of making the Manager a singleton I’d recommend, particularly with a getDefault() like factory method. Allows you to easily inject different implementations for different purposes / testing.

Best wishes, Neil

I’m still not sure I understand the initial question properly.