I'm trying to using a large number of SwingWorkers (~100). I have a class
Creature extends SwingWorker<Creature, Point>
and I want to have each instantiation to move around independently at the same time. I'm using SwingWorker since the position of each instantiation is represented in a GUI and is constantly updated. At the moment, only 10 will move and the others will not, since the max thread limit of SwingWorker is 10. The suggestion of the linked article is to put each SwingWorker in its own thread, since they implement Runnable and will allow one to bypass that limit.
I have attempted to do this but nothing shows up, so clearly I've messed it up. How do I increase the number of avaliable SwingWorkers (using the method specificed in the article or otherwise)?
Executor ex = Executors.newCachedThreadPool();
for(int i = 0; i < 20; i++){
final Creature c = new Creature(this);
Thread t = new Thread(c){
public void run(){
c.execute();
}
};
ex.execute(t);
}
I suggest you write
Executor ex = Executors.newCachedThreadPool();
for(int i = 0; i < 20; i++){
final Creature c = new Creature(this);
ex.execute(new Runnable(){
public void run(){
c.execute();
}
});
}
Or even change execute()
to run()
and make the Creature Runnable
Executor ex = Executors.newCachedThreadPool();
for(int i = 0; i < 20; i++)
ex.execute(new Creature(this));