6

मैंने सोचा कि ThreadPoolExecutor का उपयोग करके हम Runnable एस को BlockingQueue में या तो execute विधि का उपयोग करके निष्पादित करने के लिए सबमिट कर सकते हैं।
इसके अलावा मेरी समझ यह थी कि यदि कोई कार्य उपलब्ध है तो इसे निष्पादित किया जाएगा। बिल्कुलThreadPoolExecutor और queue

public class MyThreadPoolExecutor { 

    private static ThreadPoolExecutor executor; 

    public MyThreadPoolExecutor(int min, int max, int idleTime, BlockingQueue<Runnable> queue){ 
     executor = new ThreadPoolExecutor(min, max, 10, TimeUnit.MINUTES, queue); 
     //executor.prestartAllCoreThreads(); 
    } 

    public static void main(String[] main){ 
     BlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>(); 
     final String[] names = {"A","B","C","D","E","F"}; 
     for(int i = 0; i < names.length; i++){ 
      final int j = i; 
      q.add(new Runnable() { 

       @Override 
       public void run() { 
        System.out.println("Hi "+ names[j]); 

       } 
      });   
     } 
     new MyThreadPoolExecutor(10, 20, 1, q); 
     try { 
      TimeUnit.SECONDS.sleep(5); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     /*executor.execute(new Runnable() { 

      @Override 
      public void run() { 

       System.out.println("++++++++++++++"); 

      } 
     }); */ 
     for(int i = 0; i < 100; i++){ 
      final int j = i; 
      q.add(new Runnable() { 

       @Override 
       public void run() { 
        System.out.println("Hi "+ j); 

       } 
      }); 
     } 


    } 


} 

इस कोड को भी नहीं करता है कुछ भी जब तक कि मैं या तो टिप्पणी हटाएं निर्माता में executor.prestartAllCoreThreads(); या मैं runnable की execute कि प्रिंट System.out.println("++++++++++++++"); (यह भी टिप्पणी की है फोन:
क्या मुझे समझ नहीं आता पीछा कर रहा है बाहर)।

क्यों?
उद्धरण (मेरे जोर):

डिफ़ॉल्ट रूप से, यहां तक ​​कि कोर धागे शुरू में बनाई गई हैं और शुरू कर दिया केवल जब नए कार्य पहुंचें, लेकिन इस गतिशील विधि prestartCoreThread() या prestartAllCoreThreads का उपयोग कर अधिरोहित जा सकता है() । यदि आप एक गैर-खाली कतार के साथ पूल बनाते हैं तो शायद आप थ्रेड को प्रतिष्ठित करना चाहते हैं।

ठीक है। तो मेरी कतार खाली नहीं है। लेकिन मैं executor बनाता हूं, मैं sleep करता हूं और फिर मैं कतार में (0 से लूप में) Runnable एस जोड़ता हूं।
क्या यह लूप new tasks arrive के रूप में नहीं है?
यह क्यों काम नहीं करता है और मुझे या तो prestart या स्पष्ट रूप से execute पर कॉल करना है?

+1

कार्यकर्ता धागे उत्पन्न हुए हैं के रूप में कार्य 'execute' से आया, और इन लोगों कि अंतर्निहित कार्य कतार के साथ बातचीत कर रहे हैं। यदि आप एक खाली खाली कार्य कतार से शुरू करते हैं तो आपको श्रमिकों को प्रतिष्ठित करने की आवश्यकता है। [ओपनजेडीके 7 में कार्यान्वयन] देखें (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/ThreadPoolExecutor.java)। – oldrinb

+0

@veer: हाँ मुझे पता है। लेकिन मैं एक खाली खाली कतार के साथ शुरू करता हूं और फिर कतार में जोड़ना शुरू करता हूं। मुझे श्रमिकों को प्रतिष्ठित करने की आवश्यकता क्यों है? मुझे हर बार कार्यान्वयन की आवश्यकता क्यों है? या तो विनिर्देश गलत है या मैं इसे समझने में असफल रहा हूं। तो यह कौन है? – Cratylus

+0

फिर, मजदूर वे हैं जो सीधे कतार के साथ बातचीत करते हैं। वे केवल 'निष्पादन' (या इसके ऊपर की परतें, जैसे 'invokeAll',' submit', आदि) के माध्यम से पारित होने पर मांग पर उत्पन्न होते हैं। 'Execute' के लिए कोड देखें [यहां] (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/concurrent/ThreadPoolExecutor.java#1300)। – oldrinb

उत्तर

9

कार्यकर्ता धागे निष्पादित होते हैं क्योंकि कार्य निष्पादित होते हैं, और ये वे अंतर्निहित कार्य कतार के साथ बातचीत करते हैं। यदि आप एक खाली खाली कार्य कतार से शुरू करते हैं तो आपको श्रमिकों को प्रतिष्ठित करने की आवश्यकता है। implementation in OpenJDK 7 देखें।

मैं दोहराता हूं, श्रमिक वे हैं जो कार्य कतार से बातचीत करते हैं। execute के माध्यम से पारित होने पर वे केवल मांग पर पैदा हुए हैं। (या इसके ऊपर की परतें, उदाहरण के लिए invokeAll, submit, आदि) यदि वे शुरू नहीं होते हैं, तो इससे कोई फ़र्क नहीं पड़ता कि आप कतार में कितना काम जोड़ते हैं, क्योंकि के रूप में इसे जांचने के लिए कुछ भी नहीं है, कोई श्रमिक प्रारंभ नहीं कर रहा है।

ThreadPoolExecutor आवश्यक होने तक कार्यकर्ता धागे को नहीं बढ़ाता है या यदि आप prestartAllCoreThreads और prestartCoreThread विधियों द्वारा अपनी रचना को पूर्ववत करते हैं। यदि कोई कर्मचारी शुरू नहीं हुआ है, तो आपकी कतार में कोई भी काम नहीं किया जा रहा है।

कारण एक प्रारंभिक execute काम करता है ने कहा कि यह एक एकमात्र कोर कार्यकर्ता धागा है, जो तब अपनी कतार से काम पर कार्रवाई आरंभ कर के निर्माण के लिए मजबूर करता है। आप prestartCoreThread पर भी कॉल कर सकते हैं और समान व्यवहार प्राप्त कर सकते हैं। यदि आप सभी श्रमिकों को प्रारंभ करना चाहते हैं, तो आपको prestartAllCoreThreads पर कॉल करना होगा या execute के माध्यम से कार्य की संख्या सबमिट करनी होगी।

नीचे execute के लिए कोड देखें।

/** 
* Executes the given task sometime in the future. The task 
* may execute in a new thread or in an existing pooled thread. 
* 
* If the task cannot be submitted for execution, either because this 
* executor has been shutdown or because its capacity has been reached, 
* the task is handled by the current {@code RejectedExecutionHandler}. 
* 
* @param command the task to execute 
* @throws RejectedExecutionException at discretion of 
*   {@code RejectedExecutionHandler}, if the task 
*   cannot be accepted for execution 
* @throws NullPointerException if {@code command} is null 
*/ 
public void execute(Runnable command) { 
    if (command == null) 
     throw new NullPointerException(); 
    /* 
    * Proceed in 3 steps: 
    * 
    * 1. If fewer than corePoolSize threads are running, try to 
    * start a new thread with the given command as its first 
    * task. The call to addWorker atomically checks runState and 
    * workerCount, and so prevents false alarms that would add 
    * threads when it shouldn't, by returning false. 
    * 
    * 2. If a task can be successfully queued, then we still need 
    * to double-check whether we should have added a thread 
    * (because existing ones died since last checking) or that 
    * the pool shut down since entry into this method. So we 
    * recheck state and if necessary roll back the enqueuing if 
    * stopped, or start a new thread if there are none. 
    * 
    * 3. If we cannot queue task, then we try to add a new 
    * thread. If it fails, we know we are shut down or saturated 
    * and so reject the task. 
    */ 
    int c = ctl.get(); 
    if (workerCountOf(c) < corePoolSize) { 
     if (addWorker(command, true)) 
      return; 
     c = ctl.get(); 
    } 
    if (isRunning(c) && workQueue.offer(command)) { 
     int recheck = ctl.get(); 
     if (! isRunning(recheck) && remove(command)) 
      reject(command); 
     else if (workerCountOf(recheck) == 0) 
      addWorker(null, false); 
    } 
    else if (!addWorker(command, false)) 
     reject(command); 
} 
+0

आप ओपी specification.And के बारे में है जो आप JVM कार्यान्वयन के one' 'के कार्यान्वयन विवरण डाल कर जवाब देने का जवाब दे नहीं कर रहे हैं (Oracle या आईबीएम आदि के लिए अलग अलग हो सकता है) – Cratylus

+0

' कार्यकर्ता धागे कार्यों द्वारा निष्पादित आते ही उत्पन्न हुए हैं '। इसके अलावा कार्यकर्ता धागे शुरू कर रहे हैं अगर मैं सीधे कॉल करने के लिए की जरूरत के बिना कतार में कार्य जोड़ सकते हैं 'execute' – Cratylus

+0

मैं बहुत जानबूझकर एक कार्यान्वयन के कार्यान्वयन विवरण डाल (जो वास्तव में ओरेकल JDK से एक के रूप में _same_ है)। ओपी यही कारण है कि कोड काम करता है, और मैंने इसे समझाया। 'ThreadPoolExecutor' आवश्यक जब तक कार्यकर्ता धागे अंडे नहीं करता है या (तरीकों [prestartAllCoreThreads] द्वारा उनके निर्माण http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor यदि आप पहले कार्रवाई .html # prestartAllCoreThreads()) और [prestartCoreThread] (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#prestartCoreThread())। – oldrinb

5

एक ब्लॉकिंगक्यूयू एक जादू धागा प्रेषक नहीं है। यदि आप कतार में चलने योग्य वस्तुओं को सबमिट करते हैं और उन कार्यों का उपभोग करने के लिए कोई चलने वाले धागे नहीं हैं, तो निश्चित रूप से वे निष्पादित नहीं होंगे। दूसरी तरफ निष्पादन विधि थ्रेड पूल कॉन्फ़िगरेशन के अनुसार थ्रेड को स्वचालित रूप से प्रेषित कर देगी यदि इसकी आवश्यकता हो। यदि आप सभी मूल धागे को पूर्व-प्रारंभ करते हैं, तो कतार से कार्यों का उपभोग करने के लिए वहां धागे होंगे।

संबंधित मुद्दे