gpt4 book ai didi

java - ExecutorService 固定线程拒绝

转载 作者:行者123 更新时间:2023-12-01 23:01:17 24 4
gpt4 key购买 nike

我的某些线程有问题。

我的脚本

1 - 从文本文件将超过 1000 万行加载到 Array

2 - 创建一个包含 5 个固定线程的 ExecutorPool

3 - 然后它迭代该列表并向队列添加一些线程

executor.submit(new MyCustomThread(line,threadTimeout,"[THREAD "+Integer.toString(increment)+"]"));

现在 Activity 线程永远不会绕过 5 个固定线程,这很好,但我观察到我的处理器进入 100% 负载,并且我进行了一些调试,我看到 MyCustomThread 构造函数是被调用意味着无论我声明 5 个固定线程,ExecutorService 仍会尝试创建 1000 万个对象。

主要问题是:我该如何防止这种情况?我只是想让线程在没有空间的情况下被拒绝,而不是创建 1000 万个对象并一一运行它们。

第二个问题:如何获取当前的 Activity 线程?我尝试了 threadGroup.activeCount() 但它总是给我 5 5 5 5 ....

调用者类别:

System.out.println("Starting threads ...");
final ThreadGroup threadGroup = new ThreadGroup("workers");
//ExecutorService executor = Executors.newFixedThreadPool(howManyThreads);

ExecutorService executor = Executors.newFixedThreadPool(5,new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(threadGroup, r);
}
});

int increment = 0;
for(String line : arrayOfLines)
{
if(increment > 10000)
{
//System.out.println("TOO MANY!!");
//System.exit(0);
}

System.out.println(line);
System.out.println(threadGroup.activeCount());

if(threadGroup.activeCount() >= 5)
{
for(int i = 0; i < 10; i++)
{
System.out.println(threadGroup.activeCount());
System.out.println(threadGroup.activeGroupCount());
Thread.sleep(1000);
}
}


try
{
executor.submit(new MyCustomThread(line,threadTimeout,"[THREAD "+Integer.toString(increment)+"]"));
}
catch(Exception ex)
{
continue;
//System.exit(0);
}

increment++;
}

executor.awaitTermination(10, TimeUnit.MILLISECONDS);
executor.shutdown();

线程类别:

public class MyCustomThread extends Thread
{
private String ip;
private String threadName;
private int threadTimeout = 10;

public MyCustomThread(String ip)
{
this.ip = ip;
}

public MyCustomThread(String ip,int threadTimeout,String threadName)
{

this.ip = ip;
this.threadTimeout = threadTimeout;
this.threadName = threadName;

System.out.prinln("MyCustomThread constructor has been called!");
}

@Override
public void run()
{
// do some stuff that takes time ....
}
}

谢谢。

最佳答案

你这样做有点不对。执行器的理念是将工作单元实现为 Runnable 或 Callable(而不是线程)。每个 Runnable 或 Callable 应该执行一项与其他 Runnable 或 Callable 互斥的原子工作。

执行器服务在内部使用线程池,因此您创建线程组和线程没有任何好处。

尝试这个简单的作品:

ExecutorService executor = Executors.newFixedThreadPool(5);`
executor.execute(new MyRunnableWorker());

public class MyRunnableWorker implements Runnable{
private String ip;
private String threadName;
private int threadTimeout = 10;

public MyRunnableWorker(String ip){
this.ip = ip;
}

public MyRunnableWorker(String ip,int threadTimeout,String threadName){
this.ip = ip;
this.threadTimeout = threadTimeout;
this.threadName = threadName;

System.out.prinln("MyRunnableWorker constructor has been called!");
}

@Override
public void run(){ {
// do some stuff that takes time ....
}
}

这会给你你想要的。还可以尝试使用 VisualVM 测试线程代码执行,以查看线程如何运行以及负载分布。

关于java - ExecutorService 固定线程拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23345612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com