အခန်း(၅) ThreadPoolExecutor နဲ့ တွဲသုံးတဲ့ Datastructure များ(Data Structures in Action)



This content originally appeared on DEV Community and was authored by Zaw Htut Win

ThreadPoolExecutor သုံးတဲ့ Queue က Blocking Queue အမျိုးအစားဝင်တွေထဲက ဖြစ်ပါတယ်။

ExecutorService pool = Executors.newFixedThreadPool(10);

ဒါက သူ့ default constructor ကိုခေါ်ထားတာ။

return new ThreadPoolExecutor(
    nThreads, nThreads,
    0L, TimeUnit.MILLISECONDS,
    new LinkedBlockingQueue<Runnable>()
);

ThreadPoolExecutors ဆိုတာက utility class ဖြစ်ပြီးတော့ ဒီနေရာမှာ သူ့ရဲ့ Blocking Queue အမျိုးအစားက LinkedBlockingQueue ကို default အနေနဲ့သုံးတာပါ။ အထက်ဖေါ်ပြပါ code က newFixedThreadPool(10) လို့ခေါ်လိုက်ရင် ထွက်လာမယ့် Executor object ပါ။ LinkedBlockingQueue ကိုနောက်များမှ ဖေါ်ပြပါ့မယ်။

ExecutorService executor = Executors.newCachedThreadPool();

ဒါက သူ့ default constructor ကိုခေါ်ထားတာ။

return new ThreadPoolExecutor(
    0,                        // corePoolSize
    Integer.MAX_VALUE,        // maximumPoolSize
    60L, TimeUnit.SECONDS,    // idle threads die after 60s
    new SynchronousQueue<Runnable>()  // workQueue
);

ဒီတခါလည်း ThreadPoolExecutor ကို သုံးပြီး SynchronousQueue ကို default အနေနဲ့ သုံးထားတာကို တွေ့ရမှဖြစ်ပါတယ်။

ExecutorService executor = Executors.newSingleThreadExecutor();

ဒါက သူ့ default constructor ကိုခေါ်ထားတာ။

return new FinalizableDelegatedExecutorService(
    new ThreadPoolExecutor(
        1,                            // corePoolSize
        1,                            // maximumPoolSize
        0L, TimeUnit.MILLISECONDS,    // no idle timeout
        new LinkedBlockingQueue<Runnable>()  // task queue
    )
);

ဒါက ThreadPoolExecutorService ထဲက သူ့ရဲ့ implementation ဖြစ်ပါတယ်။

ပြန်ကြည့်မယ်ဆိုရင်
newFixedThreadPool က LinkedBlockingQueue
newCachedThreadPool က SynchronousQueue
newSingleThreadExecutor က LinkedBlockingQueue
ဆိုတဲ့ Queue Data Structures တွေကို သုံးသွားတာကို မြင်တွေ့နိုင်မှာဖြစ်ပါတယ်။


This content originally appeared on DEV Community and was authored by Zaw Htut Win