ExecutorService에서 shutdown ()을 호출하는 이유
나는 지난 몇 시간에 대해 상당히 읽고 있었다, 나는 단순히 어떤 이유 (볼 수없는 유효한 전화에 이유) shutdown()
온 ExecutorService
우리는 엄청난 응용 프로그램을하지 않는 한, 어떤 상점, 수십 사용되지 않는 다른 실행 프로그램 서비스의 수십 오랫동안.
(내가 모은 것에서) 셧다운이하는 유일한 일은 일단 정상 쓰레드가하는 일을하는 것입니다. 일반 Thread가 Runnable (또는 Callable)의 run 메소드를 완료하면 Garbage Collection으로 전달되어 수집됩니다. Executor Service를 사용하면 스레드가 단순히 보류 상태가되고 가비지 수집을 위해 틱되지 않습니다. 이를 위해 종료가 필요합니다.
내 질문으로 돌아갑니다. ExecutorService
자주 또는 몇 가지 작업을 제출 한 직후 에 shutdown을 호출 할 이유가 있습니까? 나는 누군가가 그것을하고있는 사건을 남겨두고 awaitTermination()
이것이 검증 된 직후에 전화를 걸고 싶습니다 . 일단 ExecutorService
그렇게하면 똑같은 일을하기 위해 새로운 것을 다시 만들어야합니다 . ExecutorService
스레드를 재사용 하는 전체 아이디어가 아닙니까? 그렇다면 왜 ExecutorService
그렇게 빨리 파괴 합니까?
단순히 생성 ExecutorService
(또는 필요한 수에 따라 커플 링) 한 다음 애플리케이션 실행 중에 작업이 수행되면 작업을 전달한 다음 애플리케이션 종료 또는 다른 중요한 단계에서 해당 실행기를 종료하는 것이 합리적이지 않습니까? ?
ExecutorServices를 사용하여 많은 비동기 코드를 작성하는 경험 많은 코더의 답변을 원합니다.
두 번째 질문, 안드로이드 플랫폼에 대한 조금 더 작은 문제입니다. 여러분 중 일부는 매번 실행기를 종료하는 것이 최선의 생각이 아니라고 말하고 Android에서 프로그래밍하는 경우 다른 이벤트를 처리 할 때 이러한 종료를 처리하는 방법 (구체적으로-실행할 때)을 알려주세요. 응용 프로그램 수명주기.
CommonsWare 의견 때문에 게시물을 중립적으로 만들었습니다. 나는 그것에 대해 죽음에 대해 논쟁하는 데 정말로 관심이 없으며 그것이 거기로 이끄는 것처럼 보입니다. 경험을 공유 할 의향이있는 경우 경험 많은 개발자에게 여기서 요청한 내용에 대해서만 관심이 있습니다. 감사.
이 shutdown()
메서드는 한 가지를 수행합니다. 클라이언트가 실행기 서비스에 더 많은 작업을 보내지 못하도록합니다. 즉, 다른 작업을 수행하지 않는 한 모든 기존 작업이 계속 실행되어 완료됩니다. 예를 들어 ScheduledExecutorService의 경우와 같이 예약 된 작업의 경우에도 마찬가지입니다. 예약 된 작업의 새 인스턴스가 실행되지 않습니다. 이는 다양한 시나리오에서 유용 할 수 있습니다.
N 개의 작업을 실행하는 실행기 서비스가있는 콘솔 애플리케이션이 있다고 가정 해 보겠습니다. 사용자가 CTRL-C를 누르면 애플리케이션이 정상적으로 종료 될 것으로 예상합니다. 우아하게 의미하는 것은 무엇입니까? 응용 프로그램이 실행기 서비스에 더 많은 작업을 제출하지 못하도록하고 동시에 기존 N 작업이 완료 될 때까지 기다리기를 원할 수 있습니다. 마지막 수단으로 종료 후크를 사용하여이를 수행 할 수 있습니다.
final ExecutorService service = ... // get it somewhere
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Performing some shutdown cleanup...");
service.shutdown();
while (true) {
try {
System.out.println("Waiting for the service to terminate...");
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
break;
}
} catch (InterruptedException e) {
}
}
System.out.println("Done cleaning");
}
}));
이 후크는 서비스를 종료하여 애플리케이션이 새 작업을 제출하지 못하도록하고 JVM을 종료하기 전에 모든 기존 작업이 완료 될 때까지 기다립니다. 대기 종료는 5 초 동안 차단되고 서비스가 종료되면 true를 반환합니다. 이 작업은 루프에서 수행되므로 서비스가 결국 종료 될 것입니다. InterruptedException은 매번 삼켜집니다. 이는 애플리케이션 전체에서 재사용되는 실행기 서비스를 종료하는 가장 좋은 방법입니다.
이 코드는 완벽하지 않습니다. 작업이 결국 종료 될 것이라고 절대적으로 확신하지 않는 한, 주어진 시간 초과를 기다렸다가 종료하고 실행중인 스레드를 포기할 수 있습니다. 이 경우 shutdownNow()
실행중인 스레드를 중단하려는 마지막 시도에서 시간 초과 후에 호출 하는 shutdownNow()
것이 좋습니다 ( 실행 대기중인 작업 목록도 제공합니다). 작업이 중단에 응답하도록 설계된 경우 제대로 작동합니다.
또 다른 흥미로운 시나리오는 정기적 인 작업을 수행하는 ScheduledExecutorService가있는 경우입니다. 주기적인 작업 체인을 중지하는 유일한 방법은를 호출하는 것 shutdown()
입니다.
편집 : 위의 일반적인 경우와 같이 종료 후크를 사용하지 않는 것이 좋습니다. 오류가 발생하기 쉬우 며 최후의 수단이어야합니다. 또한 등록 된 종료 후크가 많은 경우 실행되는 순서가 정의되지 않아 바람직하지 않을 수 있습니다. 차라리 응용 프로그램이 명시 적 shutdown()
으로 InterruptedException
.
ExecutorService가 스레드를 재사용하는 전체 아이디어가 아닙니까? 그렇다면 왜 그렇게 빨리 ExecutorService를 파괴할까요?
예. ExecutorService
자주 파괴하고 재생성해서는 안됩니다 . ExecutorService
필요할 때 (대부분 시작시) 초기화 하고 완료 될 때까지 활성 상태로 유지합니다.
단순히 ExecutorService (또는 필요한 수에 따라 몇 개)를 생성 한 다음 응용 프로그램 실행 중에 작업이 수행되면 작업을 전달한 다음 응용 프로그램 종료 또는 다른 중요한 단계에서 해당 작업을 종료하는 것이 합리적이지 않습니까? 집행자?
예. ExecutorService
애플리케이션 종료 등과 같은 중요한 단계 에서 종료 하는 것이 합리적입니다 .
두 번째 질문, 안드로이드 플랫폼에 대한 조금 더 작은 문제입니다. 여러분 중 일부는 매번 실행기를 종료하는 것이 최선의 생각이 아니라고 말하고 Android에서 프로그래밍하는 경우 응용 프로그램의 다른 이벤트를 처리 할 때 이러한 종료를 처리하는 방법 (구체적으로는 실행할 때)을 알려주세요. 수명주기.
ExecutorService
애플리케이션의 여러 활동에서 공유 된다고 가정하십시오 . 각 활동은 서로 다른 시간 간격으로 일시 중지 / 재개되며 ExecutorService
애플리케이션 당 하나씩 필요 합니다.
ExecutorService
활동 라이프 사이클 메서드 의 상태를 관리하는 대신 ExecutorService 관리 (생성 / 종료)를 사용자 지정 서비스로 이동 합니다.
ExecutorService
서비스 =>에서 생성 onCreate()
하고 올바르게 종료하십시오.onDestroy()
권장 종료 방법 ExecutorService
:
Java ExecutorService를 올바르게 종료하는 방법
An ExecutorService should be shut down once it is no longer needed to free up system resources and to allow graceful application shutdown. Because the threads in an ExecutorService may be nondaemon threads, they may prevent normal application termination. In other words, your application stays running after completing its main method.
Chaper:14 Page:814
Reason for calling shutdown() on ExecutorService
Today I encountered a situation where I have to wait until a machine is ready, before starting a series of tasks on that machine.
I make a REST call to this machine, if I don't receive 503 (Server Unavailable) then the machine is ready to process my requests. So, I wait until I get 200 (Success) for the first REST call.
There are multiple ways to achieve it, I used ExecutorService to create a thread and scheduled it to run after every X Seconds. So, I need to stop this thread on a condition, check this out...
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
try {
int statusCode = restHelper.firstRESTCall();
if (statusCode == 200) {
executor.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
};
int retryAfter = 60;
executor.scheduleAtFixedRate(task, 0, retryAfter, TimeUnit.SECONDS);
Second side question, a bit smaller deals with android platform.
Maybe I can answer if you'll provide bit more context! Also from my experience with Android development it's rarely you need Threads. Are you developing a Game or an app which needs threads for performance? If not, in Android you have other ways to tackle problems like the scenario that I explained above. You can rather use TimerTask, AsyncTask or Handlers or Loaders based on context. This is because if UIThread waits for long you know what happens :/
This is genuine notwithstanding for planned undertakings, e.g., for a ScheduledExecutorService: new cases of the booked assignment won't run.
We should expect you have a comfort application which has an agent administration running N errands.
I'm not catching it's meaning effortlessly? Perhaps you need your application to not have the option to submit more assignments to the agent administration and in the meantime you need to sit tight for your current N undertakings to finish.
Except if you're totally positive your errands will in the end, you should need to sit tight for a given break and after that simply exit, deserting the running strings.
In the event that your activitys are intended to react to interference this will work fine.
Another intriguing situation is the point at which you have a ScheduledExecutorService that plays out an activity.
The best way to stop the chain of activity is to call shutdown()
참고URL : https://stackoverflow.com/questions/16122987/reason-for-calling-shutdown-on-executorservice
'IT TIP' 카테고리의 다른 글
Scala의 forSome 키워드는 무엇입니까? (0) | 2020.11.05 |
---|---|
grep --ignore-case --only (0) | 2020.11.05 |
새로운 netstandardapp과 netcoreapp TFM의 차이점은 무엇입니까? (0) | 2020.11.05 |
CFPrefsPlistSource iOS 10에서 값을 읽지 못했습니다. (0) | 2020.11.05 |
C #에서 이름이있는 값 튜플에 별칭을 정의 할 수 있습니까? (0) | 2020.11.05 |