Pelaksana
![](https://cdn.codegym.cc/images/article/83e7f607-1195-4d00-a2b8-f790515dc1a9/800.jpeg)
Executor minangka antarmuka dhasar kanggo kelas sing ngetrapake peluncuran tugas Runnable . Iki menehi pitulung kanggo nambah tugas lan carane miwiti.
ExecutorService minangka antarmuka sing ngluwihi sifat Executor lan nggambarake layanan kanggo mbukak tugas Runnable utawa Callable . Cara ngirim nampa tugas ing wangun Callable utawa Runnable minangka input , lan Future digunakake minangka nilai bali, liwat kang bisa njaluk asil.
Cara invokeAll tanggung jawab kanggo nglakokake tugas, ngasilake dhaptar tugas kanthi status lan asil rampung.
Cara invokeAny tanggung jawab kanggo nglakokake tugas, ngasilake asil tugas sing wis rampung (yaiku, tanpa mbuwang pengecualian), yen ana.
ScheduledExecutorService - antarmuka iki nambah kemampuan kanggo mbukak tugas sing ditundha kanthi wektu tundha tartamtu utawa wektu tartamtu.
AbstractExecutorService minangka kelas abstrak kanggo mbangun ExecutorService 'a. Ing njero ana implementasi metode submit , invokeAll , invokeAny . Kelas iki duweni ThreadPoolExecutor , ScheduledThreadPoolExecutor lan ForkJoinPool .
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
Callable<String> task = () -> {
System.out.println(Thread.currentThread().getName());
return Thread.currentThread().getName();
};
scheduledExecutorService.schedule(task, 10, TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
}
ThreadPoolExecutor
![](https://cdn.codegym.cc/images/article/2b86fe3b-e258-4219-8c32-7d3ba93670ea/800.jpeg)
Executors minangka kelas pabrik kanggo nggawe ThreadPoolExecutor , ScheduledThreadPoolExecutor . Yen sampeyan kudu nggawe salah siji saka pools iki, pabrik iki persis apa sing perlu. Isine macem-macem adaptor Runnable-Callable, PrivilegedAction-Callable, PrivilegedExceptionAction-Callable lan liya-liyane. Nduwe cara statis kanggo nggawe ThreadPool sing beda .
ThreadPoolExecutor - Ngleksanakake antarmuka Executor lan ExecutorService lan misahake nggawe tugas saka eksekusi tugas. Kita kudu ngleksanakake obyek Runnable lan ngirim menyang pelaksana, lan ThreadPoolExecutor tanggung jawab kanggo eksekusi, instantiation lan karya karo Utas.
ScheduledThreadPoolExecutor - Saliyane cara ThreadPoolExecutor , nggawe blumbang thread sing bisa gawe jadwal printah kanggo eksekusi sawise wektu tundha tartamtu utawa kanggo eksekusi mesti.
ThreadFactoryminangka obyek sing nggawe benang anyar sing dikarepake. Kita kudu ngirim conto menyang metode Executors.newSingleThreadExecutor(ThreadFactory threadFactory) .
ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "MyThread");
thread.setPriority(Thread.MAX_PRIORITY);
return thread; }
});
RejectedExecutionHandler - ngijini sampeyan kanggo nemtokake handler kanggo tugas sing sakperangan alesan ora bisa kaleksanan liwat ThreadPoolExecutor . Iki kedadeyan nalika ora ana aliran gratis utawa layanan mati utawa mati.
Sawetara implementasi standar ditemokake ing kelas ThreadPoolExecutor :
- CallerRunsPolicy - mbukak tugas ing thread nelpon;
- AbortPolicy - mbuwang pangecualian;
- DiscardPolicy - nglirwakake tugas;
- DiscardOldestPolicy - Mbusak tugas paling tuwa sing durung diwiwiti saka antrian, banjur nyoba nambah tugas anyar maneh.
Layanan Rampung
![](https://cdn.codegym.cc/images/article/52be544f-17a5-406c-9f5f-9864056f87a8/512.jpeg)
CompletionService minangka antarmuka layanan kanthi decoupling ngluncurake tugas sing ora sinkron lan entuk asil. Kanggo nambah tugas, ana carangirim, lan kanggo entuk asil saka tugas sing wis rampung,njupukpollingnon-blocking.
ExecutorCompletionService minangka pambungkus ing sembarang kelas sing ngleksanakake antarmuka Executor , kayata ThreadPoolExecutor utawa ForkJoinPool . Iki digunakake nalika perlu abstrak saka cara ngluncurake tugas lan ngontrol eksekusi.
Yen ana tugas rampung, banjur kita tarik metu. Yen ora ana tugas, banjur kita gantung nganti rampung. Layanan kasebut nggunakake LinkedBlockingQueue ing inti, nanging sampeyan bisa ngliwati implementasi BlockingQueue.