java:需要在不使用承包商的情况下将 arg 传递给线程 运行()
java: need to pass arg to thread run() without using the contractor
我想要这样的东西:
public class WorkerThread implements Runnable {
int y;
public WorkerThread(int y) {
this.y = y;
}
@Override
public void run(int x) {
System.out.println(x+y);
}
}
主要
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int num: number) {
Runnable worker = new WorkerThread(num);
for (int anotherNum : anotherNumbers) {
executor.execute(worker.run(anotherNum));
}
}
我想为每个数字创建线程,并且我想 运行 它带有另一个数字的参数。
我不能这样做,因为 运行() 在其签名中没有参数。
我还有哪些其他选择?
拜托,不要问我为什么需要它或试图质疑我在这里使用线程的原因。
你试图做到这一点是不可能的。你会想要这样做:
class WorkerThread implements Runnable {
int x, y;
public WorkerThread(int y) {
this.y = y;
}
public void run() {
System.out.println(x+y);
}
}
然后,当您将代码调用到 运行 时:
for (int num: number) {
WorkerThread worker = new WorkerThread(num); //worker.y = num
for (int anotherNum : anotherNumbers) {
worker.x = anotherNum; //we assign x here
executor.execute(worker); //You call by the object, not by its method
}
}
编辑:我将 worker 更改为 WorkerThread 类型,因为 executor.execute(Runnable) 将获取一个对象而无需显式转换为 Runnable,只要该对象有问题的是 Runnable 的实现者。
双重编辑组合: 让我们尝试一种不同的方法,假设您使用的是 Java 8:
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int num : numbers) {
Integer x = num;
for(int num2 : otherNumbers) {
Integer y = num2;
executor.execute(() -> System.out.println(x+y));
}
}
这将具有您之前代码的精确输出,而无需定义整个 class 只是为了执行任务。
public class WorkerThread implements Runnable {
int y;
int x;
public WorkerThread(int y, int x) {
this.y = y;
}
public void setX (int x)
{
this.x=x;
}
@Override
public void run(int x) {
System.out.println(x+y);
}
}
然后你从循环中调用 setX 方法,但是你的代码无论如何都不会工作,因为 executor.execute(worker.run(anotherNum));
需要一个 Runnable,并且由于 运行 是无效的,它甚至不会编译,我建议你做正在关注
for (int anotherNum : anotherNumbers) {
Worker.setX(anotherNumbers);
executor.execute(worker);
}
我想要这样的东西:
public class WorkerThread implements Runnable {
int y;
public WorkerThread(int y) {
this.y = y;
}
@Override
public void run(int x) {
System.out.println(x+y);
}
}
主要
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int num: number) {
Runnable worker = new WorkerThread(num);
for (int anotherNum : anotherNumbers) {
executor.execute(worker.run(anotherNum));
}
}
我想为每个数字创建线程,并且我想 运行 它带有另一个数字的参数。
我不能这样做,因为 运行() 在其签名中没有参数。
我还有哪些其他选择?
拜托,不要问我为什么需要它或试图质疑我在这里使用线程的原因。
你试图做到这一点是不可能的。你会想要这样做:
class WorkerThread implements Runnable {
int x, y;
public WorkerThread(int y) {
this.y = y;
}
public void run() {
System.out.println(x+y);
}
}
然后,当您将代码调用到 运行 时:
for (int num: number) {
WorkerThread worker = new WorkerThread(num); //worker.y = num
for (int anotherNum : anotherNumbers) {
worker.x = anotherNum; //we assign x here
executor.execute(worker); //You call by the object, not by its method
}
}
编辑:我将 worker 更改为 WorkerThread 类型,因为 executor.execute(Runnable) 将获取一个对象而无需显式转换为 Runnable,只要该对象有问题的是 Runnable 的实现者。
双重编辑组合: 让我们尝试一种不同的方法,假设您使用的是 Java 8:
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int num : numbers) {
Integer x = num;
for(int num2 : otherNumbers) {
Integer y = num2;
executor.execute(() -> System.out.println(x+y));
}
}
这将具有您之前代码的精确输出,而无需定义整个 class 只是为了执行任务。
public class WorkerThread implements Runnable {
int y;
int x;
public WorkerThread(int y, int x) {
this.y = y;
}
public void setX (int x)
{
this.x=x;
}
@Override
public void run(int x) {
System.out.println(x+y);
}
}
然后你从循环中调用 setX 方法,但是你的代码无论如何都不会工作,因为 executor.execute(worker.run(anotherNum));
需要一个 Runnable,并且由于 运行 是无效的,它甚至不会编译,我建议你做正在关注
for (int anotherNum : anotherNumbers) {
Worker.setX(anotherNumbers);
executor.execute(worker);
}