限制在java中执行的最大线程数 [英] limit maximum threads executing at time in java

查看:190
本文介绍了限制在java中执行的最大线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚Java中的线程如何工作,我只是想通过将它们全部放入数组来限制可运行线程执行,然后检查循环中是否有一些完成并弹出它们,以便有可能生成一个新线程,在此代码中出现异常:

Trying to figure out how threading in Java works, i just want to limit runnable threads executing by putting them all to array and then check in loop if some of them finished and pop them out, to have possibility to spawn a new thread, got exception in this code:

public class testThread implements Runnable {
public void run () {
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e){}
        System.out.println("This is the test thread");
    }   

    public static void main (String args[]) {
        int max_threads = 5;
        Thread worker;
        ArrayList<Thread> all_workers = new ArrayList<Thread>(max_threads   );
        for (int i =0; i<50; i++) {
            if (all_workers.size()<max_threads){
                worker = new Thread (new testThread());
                all_workers.add(worker);
                worker.start(); 
            } else{
                System.out.println("i ran all");
                while(all_workers.size()>=max_threads){
                    try{ 
                        System.out.println("Waiting for some to finish");
                        int counter = 0;
                        for (Thread wrk: all_workers){
                            if (!wrk.isAlive()){
                                all_workers.remove(counter);
                            }
                            counter ++ ;
                        }
                        Thread.sleep(500);
                    } catch (InterruptedException e){
                        System.out.println("Catched unhandled ");
                    }
                }
            }
        }

        for(Thread wrk: all_workers){
            try {
                wrk.join();
            } catch (InterruptedException e) {
            }
        }
    }
}

运行时得到的异常:

anybody@anymachine ~/java $ java testThread 
i ran all
Waiting for some to finish
Waiting for some to finish
This is the test thread
This is the test thread
This is the test thread
This is the test thread
This is the test thread
Waiting for some to finish
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at testThread.main(testThread.java:39)

感谢您的任何帮助,如果有一个很好的教程,我会非常感兴趣的链接。

thank you for any help, if there is a good tutorial i would be much appriciated for link.

PS。如果在Python中有像pdb中的任何调试器请告诉我。
谢谢!

PS. if there is any debugger in java like pdb in python please let me know. thank you!

推荐答案

您应该查看更高级别的线程实用程序,如ExecutorService和ThreadPools。

You should have a look at the higher level threading utilities like ExecutorService and ThreadPools.

你永远不应该手动终止线程,我建议一般不要手动创建/管理线程。

You should never terminate a thread manually and I suggest to avoid manual thread creation/management in general.

如果你想要等待多个线程完成,你可能想要使用CountDownLatch。
这里有一个例子

If you want to wait for a number of threads finishing, you may want to use a CountDownLatch. Here is an example.

这篇关于限制在java中执行的最大线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆