메소드에서 두 가지 유형의 변수 (Future, ExecutorService)를 리턴해야합니다. 나는 어떻게 움직일 수 있니? 여기에 코드 조각이 있습니다. 클래스 A가 있는데 'B'클래스에서 run() 메서드를 호출하려고합니다. 'threadExecutor.shutdownNow()'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();
}
}
I 선에서'stopExecutorService '방식에서'NullPointerException이 '점점 미터 정지 방법 이 오류의 이유는 threadExecutor 값이 main 메소드에서 변경되지 않았기 때문입니다. 'startExecutorService'메소드에서만 변경되었습니다. 그래서
이 친절
태그의 문제 상황에 대한 힌트를 넣을 수도 있습니다. 이것이 Java의 동시성과 관련된 것이라면 "java", "concurrency"또는 "multithreading"태그를 추가해야합니다. 인기있는 태그를 추가하면 더 많은 관심을 끌게됩니다. 즉, 나는 당신을 도울 방법을 모른다. –
고맙습니다. 내가 할께. – Vikram