Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用大量并发Swingworker_Java_Multithreading_Concurrency_Threadpool_Swingworker - Fatal编程技术网

Java 使用大量并发Swingworker

Java 使用大量并发Swingworker,java,multithreading,concurrency,threadpool,swingworker,Java,Multithreading,Concurrency,Threadpool,Swingworker,我正在尝试使用大量的Swingworker(~100)。我有一节课 Creature extends SwingWorker<Creature, Point> 生物扩展SwingWorker 我想让每个实例化同时独立地移动。我使用SwingWorker是因为每个实例化的位置都在GUI中表示,并且不断更新。目前,只有10个会移动,其他的不会,因为链接文章的建议是将每个SwingWorker放在自己的线程中,因为它们实现Runnable并允许一个绕过该限制 我曾尝试过这样做,但什么也

我正在尝试使用大量的Swingworker(~100)。我有一节课

Creature extends SwingWorker<Creature, Point>
生物扩展SwingWorker
我想让每个实例化同时独立地移动。我使用SwingWorker是因为每个实例化的位置都在GUI中表示,并且不断更新。目前,只有10个会移动,其他的不会,因为链接文章的建议是将每个SwingWorker放在自己的线程中,因为它们实现Runnable并允许一个绕过该限制

我曾尝试过这样做,但什么也没有出现,所以很明显我把事情搞砸了。如何增加可用Swingworker的数量(使用本文中指定的方法或其他方法)

Executor ex=Executors.newCachedThreadPool();
对于(int i=0;i<20;i++){
最终生物c=新生物(此);
螺纹t=新螺纹(c){
公开募捐{
c、 执行();
}
};   
execute(t);
} 
我建议你写

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();  
      }
   });   
} 

不要将线程传递给执行器。改为传递Runnable。@PeterLawrey您的意思是
Runnable t=new Thread(c){…}
?我只是尝试了一下,它不起作用,我的意思是根本不创建线程。如果这不起作用,你就做错了
Executor.execute(Runnable)
需要一个Runnable。@PeterLawrey啊,这很好。好建议。如果你在回答中坚持这一点,我会接受的
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();  
      }
   });   
} 
Executor ex = Executors.newCachedThreadPool();
for(int i = 0; i < 20; i++)
   ex.execute(new Creature(this));