2013-09-10 4 views
27

मैं exectorServices का उपयोग threads पूल करने और कार्यों को भेजने के लिए सीख रहा हूं। मैं नीचेअंतर?

import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.TimeUnit; 


class Processor implements Runnable { 

    private int id; 

    public Processor(int id) { 
     this.id = id; 
    } 

    public void run() { 
     System.out.println("Starting: " + id); 

     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      System.out.println("sorry, being interupted, good bye!"); 
      System.out.println("Interrupted "+Thread.currentThread().getName()); 
      e.printStackTrace();  
     } 

     System.out.println("Completed: " + id); 
    } 
} 


public class ExecutorExample { 

    public static void main(String[] args) { 
     Boolean isCompleted=false; 

     ExecutorService executor = Executors.newFixedThreadPool(2); 

     for(int i=0; i<5; i++) { 
      executor.execute(new Processor(i)); 
     } 

     //executor does not accept any more tasks but the submitted tasks continue 
     executor.shutdown(); 

     System.out.println("All tasks submitted."); 

     try { 
      //wait for the exectutor to terminate normally, which will return true 
      //if timeout happens, returns false, but this does NOT interrupt the threads 
      isCompleted=executor.awaitTermination(100, TimeUnit.SECONDS); 
      //this will interrupt thread it manages. catch the interrupted exception in the threads 
      //If not, threads will run forever and executor will never be able to shutdown. 
      executor.shutdownNow(); 
     } catch (InterruptedException e) { 
     } 

     if (isCompleted){ 
     System.out.println("All tasks completed."); 
     } 
     else { 
      System.out.println("Timeout "+Thread.currentThread().getName()); 
     } 
    } 
     } 

एक सरल कार्यक्रम है यह कल्पना कुछ नहीं करता है, लेकिन दो threads बनाता है और कुल 5 कार्य सबमिट करता है। प्रत्येक thread अपने कार्य को पूरा करने के बाद, यह अगले एक, ऊपर दिए गए कोड में, मैं executor.submit का उपयोग करता हूं। मैं भी executor.execute में बदल गया। लेकिन मुझे आउटपुट में कोई अंतर नहीं दिख रहा है। submit and execute विधियों किस तरह से भिन्न हैं? यह वही है API कहते

विधि प्रस्तुत फैली बनाने और भविष्य कि निष्पादन रद्द करने और/या पूरा करने के लिए प्रतीक्षा करने के लिए इस्तेमाल किया जा सकता वापस लौट कर आधार विधि Executor.execute (java.lang.Runnable)। विधियों का आह्वान करें किसी भी और invoke सभी कार्यों के संग्रह को निष्पादित करने, और कम से कम एक, या सभी के लिए इंतजार कर, थोक निष्पादन के सबसे आम रूपों का प्रदर्शन करते हैं। (क्लास एक्जिक्यूटर कॉम्प्लिशन सेवा का उपयोग इन विधियों के कस्टमाइज्ड वेरिएंट लिखने के लिए किया जा सकता है।)

लेकिन यह मेरे लिए स्पष्ट नहीं है कि इसका क्या अर्थ है? धन्यवाद

+0

उपयोगी सवाल। इसे यहां पोस्ट करने का मूल्य है। – MKod

उत्तर

29

जैसा कि आप JavaDoc execute(Runnable) से देखते हैं, कुछ भी वापस नहीं आता है।

हालांकि, submit(Callable<T>) एक Future वस्तु जो आप प्रोग्राम के चल रहे धागा रद्द करने के लिए बाद में और साथ ही T जाता है कि जब लौटे Callable कम्प्लिट्स पाने के लिए एक तरह से की अनुमति देता है देता है। अधिक जानकारी के

Future<?> future = executor.submit(longRunningJob); 
... 
//long running job is taking too long 
future.cancel(true); 

इसके अलावा के लिए JavaDoc of Future देखें, अगर future.get() == null और किसी भी अपवाद तो Runnable सफलतापूर्वक

30

अंतर यह है कि execute बस किसी भी आगे की हलचल के बिना काम शुरू होता है, जबकि submit रिटर्न कार्य प्रबंधन के लिए एक Future वस्तु है। आप Future वस्तु के साथ निम्नलिखित बातें कर सकते हैं:

  • रद्द करें काम समय से पहले ही, cancel विधि के साथ।
  • get के साथ कार्य निष्पादन समाप्त करने के लिए कार्य की प्रतीक्षा करें।

Future इंटरफ़ेस अधिक उपयोगी है यदि आप पूल में Callable सबमिट करते हैं। Future.get पर कॉल करते समय call विधि का वापसी मूल्य वापस कर दिया जाएगा। यदि आप Future का संदर्भ नहीं रखते हैं, तो कोई अंतर नहीं है।

3

निष्पादित जमा फेंक नहीं है - भविष्य वस्तु है, जो प्रस्तुत कार्य का परिणाम जाँच करने के लिए इस्तेमाल किया जा सकता देता है। रद्द करने या चेक करने के लिए इस्तेमाल किया जा सकता है आदि

निष्पादन - कुछ भी वापस नहीं करता है।

5

execute: आग के लिए यह प्रयोग करें और कॉल

submit: विधि कॉल का परिणाम का निरीक्षण किया और Future पर उचित कार्रवाई करने के लिए इसका उपयोग करें भूल आपत्ति की कॉल

मेजर अंतर से लौटे: Exception निपटने

submit() फ्रेमवर्क में स्वयं को संभाला Exception छुपाता है।

execute() अन-हैंडल Exception फेंकता है।

submit()

  1. साथ निपटने अपवाद के लिए समाधान लपेटें अपने Callable or Runnable code in try{} catch{} block

    या

  2. रखें future.get() call in try{} catch{} block

    या

  3. अपनी खुद की ThreadPoolExecutor और ओवरराइड afterExecute तरीका लागू

invokeAll पर दौरे अन्य प्रश्नों के बारे में:

पर क्रियान्वित दिया कार्यों, फ्यूचर्स की एक सूची लौट उनकी स्थिति और परिणाम जब सभी पकड़े पूरा या टाइमआउट समाप्त हो जाता है, जो भी पहले होता है।

invokeAny:

, यह देखते हुए कार्यों को कार्यान्वित करता है, एक है कि सफलतापूर्वक है (यानी एक अपवाद फेंकने के बिना) पूरा कर लिया है का परिणाम लौटने अगर किसी भी समय समाप्त समाप्त होने से पहले करते हैं।

invokeAll का उपयोग करें यदि आप सभी सबमिट किए गए कार्यों को पूरा करने के लिए प्रतीक्षा करना चाहते हैं।

invokeAny का उपयोग करें यदि आप एन सबमिट किए गए कार्यों में से एक कार्य के सफल समापन की तलाश में हैं। इस मामले में, कार्यों में से एक सफलतापूर्वक पूर्ण होने पर प्रगति पर कार्य रद्द कर दिए जाएंगे। कोड उदाहरण के साथ

संबंधित पोस्ट:

Choose between ExecutorService's submit and ExecutorService's execute