gpt4 book ai didi

JAVA将方法从外部类传递到ThreadPool.submit()

转载 作者:行者123 更新时间:2023-11-30 07:43:06 24 4
gpt4 key购买 nike

我以前没有 JAVA 并发的经验,但在 C# 中做过同样的事情。

我的任务创建一个“worker”类,以便在我的应用程序中轻松管理多线程(创建连续线程)。我想要的结果(使用示例):

Worker worker = new Worker();
worker.threadCount = 10;
worker.doWork(myMethod);
worker.Stop();

能够在我的应用程序的任何类中使用它,接受“void”方法作为“worker.doWork(myMethod);”论证。

我对这个问题的研究做了什么:

worker 类(Class)

package commons.Threading;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class Worker {
static Boolean IsRunning = true;
public static int threadCount = 2;
static ExecutorService threadPool = new ErrorReportingThreadPoolExecutor(threadCount);

public void doWork(**argument method**) throws IOException, InterruptedException {

while (IsRunning) {
threadPool.submit(new Runnable() {
**argument method**
});

Thread.sleep(1000);
}
}


public static void Stop(){
IsRunning = false;
threadPool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {
threadPool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!threadPool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
threadPool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}

ErrorReportingThreadPoolExecutor

package commons.Threading;

import java.util.concurrent.*;

public class ErrorReportingThreadPoolExecutor extends ThreadPoolExecutor {
public ErrorReportingThreadPoolExecutor(int nThreads) {
super(nThreads, nThreads,
0, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}

@Override
protected void afterExecute(Runnable task, Throwable thrown) {
super.afterExecute(task, thrown);

if (thrown != null) {
// an unexpected exception happened inside ThreadPoolExecutor
thrown.printStackTrace();
}

if (task instanceof Future<?>) {
// try getting result
// if an exception happened in the job, it'll be thrown here
try {
Object result = ((Future<?>)task).get();
} catch (CancellationException e) {
// the job get canceled (may happen at any state)
e.printStackTrace();
} catch (ExecutionException e) {
// some uncaught exception happened during execution
e.printStackTrace();
} catch (InterruptedException e) {
// current thread is interrupted
// ignore, just re-throw
Thread.currentThread().interrupt();
}
}
}

public static void main(String[] args) throws InterruptedException {
// replace
// ExecutorService threadPool = Executors.newFixedThreadPool(2);
// with
ExecutorService threadPool = new ErrorReportingThreadPoolExecutor(2);

while (true) {
threadPool.submit(new Runnable() {
@Override public void run() {
System.out.println("Job is running...");

if (Math.random() < 0.5) {
int q = 1 / 0;
}

System.out.println("Job finished.");
}
});

Thread.sleep(1000);
}
}
}

所以,问题是 - 我如何从外部类传递“void”方法 threadPool.submit(new Runnable() { here });

最佳答案

你可以传递Runnable本身作为一个参数,

public void doWork(Runnable runnable) throws IOException, InterruptedException {

while (IsRunning) {
threadPool.submit(runnable);

Thread.sleep(1000);
}
}

Runnable 是一个函数式接口(interface),它有一个方法run,它接受无参数并返回void,因此您可以使用它作为一个函数。

Runnable runnable = new Runnable(){

public void run(){
// do work
}
};
doWork(runnable);

如果你使用的是Java 1.8,可以更简洁地表达

Runnable runnable = ()->{/**do work*/};
doWork(runnable);

关于JAVA将方法从外部类传递到ThreadPool.submit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380999/

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