我需要从一个方法返回两种不同类型的变量(Future、ExecutorService)。我该怎么走??这是一段代码。我有一个 A 类,我试图在“B”类中调用 run() 方法。“startExecutorService”是启动threadExecutor的方法,“stopExecutorService”是停止threadExecutor的方法
public class A{
public static void main(String[] args){
A AObj= new A();
Future<?> task = null;
ExecutorService threadexec = null;
task = AObj.startExecutorService(task, threadexec);
AObj.stopExecutorService(task, threadexec);
}
public Future<?> startExecutorService(Future<?> control, ExecutorService threadExecutor){
//'noOfThreads' determines the total no of threads to be created in threadpool
int noOfThreads = 1;
// Creating an object for class 'B' which has the run() method
B ThreadTaskOne = new ExecutorServiceThreadClass();
// Calling the ExecutorService to create Threadpool with specified number of threads
threadExecutor = Executors.newFixedThreadPool(noOfThreads);
// Creating a Future object 'control' and thereby starting the thread 'ThreadTaskOne'
control = threadExecutor.submit(ThreadTaskOne);
return control;
}
public void stopExecutorService(Future<?> task, ExecutorService threadExecutor){
// Interrupting the thread created by threadExecutor
task.cancel(true);
if(task.isCancelled()){
System.out.println("Task has been cancelled !!");
}
// Closing the threadExecutor
threadExecutor.shutdownNow();
}
}
我在'threadExecutor.shutdownNow();'行的'stopExecutorService'方法中得到'NullPointerException',这个错误的原因是因为threadExecutor值在main方法中没有改变..它只在'startExecutorService'方法中改变。所以我想将 threadExecutor 的更改值连同 Future 一起返回给 main 方法。
请帮帮我