2012-10-18 8 views
0

메소드에서 두 가지 유형의 변수 (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'메소드에서만 변경되었습니다. 그래서

이 친절

+0

태그의 문제 상황에 대한 힌트를 넣을 수도 있습니다. 이것이 Java의 동시성과 관련된 것이라면 "java", "concurrency"또는 "multithreading"태그를 추가해야합니다. 인기있는 태그를 추가하면 더 많은 관심을 끌게됩니다. 즉, 나는 당신을 도울 방법을 모른다. –

+0

고맙습니다. 내가 할께. – Vikram

답변

2

복합 클래스를 확인하고 해당 클래스의 객체를 반환 나를 도와 .. 미래와 함께, 주요 방법으로 다시 threadExecutor의 변경된 값 을 반환하려면?

+0

고마워 ..하지만 다른 클래스에 가지 않고 A 클래스 내 제안을 할 수 있니? 그게 내 요구 사항이야. – Vikram

+0

좋아요. , ExecutorService> .. 그리고 그 목록을 반환하는 메소드 만들기 .. 그 어떤 제안이라도 내가 찾고있는 것이다. – Vikram

관련 문제