gpt4 book ai didi

java - 如何将参数传递给 ScheduledThreadPoolExecutor?

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

我如何将参数传递给 ScheduledThreadPoolExecutor?

我有以下代码。您会注意到,我声明了一个变量“num”,并将其作为参数传递给 exampleFunction()。 exampleFunction 包含一个 ScheduledThreadPoolExecutor。我希望能够在 public void run() 中使用变量“num”。有什么办法可以做到这一点吗?

     class Test {
...
int num;
exampleFunction(num);
...

public void exampleFunction(num) {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.schedule(new Runnable() {
public void run() {
...do something here...
...something with 'num' here...
...i get an error when i try to use 'num' here
}
}, 10, TimeUnit.SECONDS);
}

}

最佳答案

您是否尝试将 exampleFunction(num) 更改为 exampleFunction(final int num)?由于 run 方法位于内部类中,因此所有外部绑定(bind)都需要是最终的。

public void exampleFunction(final int num) { // final int here
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.schedule(new Runnable() {
public void run() {
...do something here...
...something with 'num' here...
...i get an error when i try to use 'num' here
}
}, 10, TimeUnit.SECONDS);
}

关于java - 如何将参数传递给 ScheduledThreadPoolExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15990378/

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