In my program I wrote a class, which updates a bunch of listeners every time when a new value is triggered. Therefor I am using a SingleThreadExecutor, the example should make it more clear. What I am worrying about is, does it make sense when I run it like that:
Class Update() {
ExecutorService svc = Executors.newSingleThreadExecutor();
svc.execute(new Runnable() {
public void run() {
if(!theListeners.isEmpty()) {
for(IgpsdListener l : theListeners) {
l.update(jsonObject);
}
}
}
});
I heard, that creating Threads is quite expensive action in JAVA, so I am wondering if I am using it correctly. I mean, it creates every time a new Runnable object right? Even the Executor uses only the one and only thread.
Thanks in advance for any help and enlightenment.
nyyrikki
This is exactly what you’re supposed to be doing. Java is quite fast at creating one-shot objects like this; threads might be expensive, but just creating a
Runnablelike you do here is cheap.