2011-10-17 17 views
6

मैं प्राथमिकता के साथ ThreadPoolExecutor बनाने की कोशिश कर रहा हूं। तो मैंजावा में प्राथमिकता ThreadPoolExecutor (एंड्रॉइड)

private static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, MAXPOOL, TimeUnit.SECONDS, 
    queue, new mThreadFactory()); 

परिभाषित करता है तो कुंजी अब कतार संदर्भ है। लेकिन जब मैं घोषणा: एक ही QuickFix साथ (पूर्णांक, पूर्णांक, पूर्णांक, TIMEUNIT, PriorityBlockingQueue, FileAccess.mThreadFactory) है अपरिभाषितनिर्माता ThreadPoolExecutor:

static PriorityBlockingQueue<mDownloadThread> queue=new PriorityBlockingQueue<mDownloadThread>(MAXPOOL,new DownloadThreadComparator()); 

संकलक पहली पंक्ति में एक त्रुटि देता है BlockingQueue पर 'कतार' का प्रकार बदलें। क्या आप समझने में मेरी मदद कर सकते हैं कि समस्या क्या है?

धन्यवाद

अतिरिक्त जानकारी:

class DownloadThreadComparator implements Comparator<mDownloadThread>{ 
    @Override 
    public int compare(mDownloadThread arg0, mDownloadThread arg1) { 

    if (arg0==null && arg1==null){ 
     return 0; 
    } else if (arg0==null){ 
     return -1; 
    } else if (arg1==null){ 
     return 1; 
    } 
    return arg0.getPriority()-arg1.getPriority(); 

    } 

} 

उत्तर

6

ThreadPoolExecutor निर्माता स्वीकार करता BlockingQueue<Runnable> और नहीं:

runnables की तुलना के लिए मैं निम्नलिखित वर्ग

class mDownloadThread implements Runnable{  
    private Runnable mRunnable;  
    private int priority;  
    mDownloadThread(Runnable runnable){ 
     mRunnable=runnable;   
    } 

    @Override 
    public void run() { 
     mRunnable.run();   
    } 

    public int getPriority() { 
     return priority; 
    } 

    public void setPriority(int priority) { 
     this.priority = priority; 
    }  
} 

तुलनित्र implementd, इस प्रकार आप इसे PriorityBlockingQueue<mDownloadThread> उदाहरण पास नहीं कर सकते हैं।

आप PriorityBlockingQueue<Runnable> को queue का प्रकार बदल सकते हैं, लेकिन इस मामले में आप compare विधि के अंदर कास्टिंग बिना Comparator<mDownloadThread> को लागू करने में सक्षम नहीं होगा।

अन्य समाधान सामान्य प्रकार-चेकिंग बायपास करने के लिए है, लेकिन यह execute विधि के लिए mDownloadThread का ही उदाहरण हैं प्रस्तुत करने के लिए अपनी जिम्मेदारी होगा:

static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, 
     MAXPOOL, TimeUnit.SECONDS, (PriorityBlockingQueue) queue, new mThreadFactory()); 
4

समस्या यह है कि ThreadPoolExecutor एक BlockingQueue<Runnable> उम्मीद कर रही है और आप गुजर रहे हैं है एक PriorityBlockingQueue<mDownloadThread>PriorityBlockingQueue लागू BlockingQueue ताकि समस्या नहीं है। समस्या Runnable है। आप mDownloadThread का सामान्य प्रकार नहीं पारित कर सकते हैं जहां Runnable अपेक्षित था।

फिक्स निम्नलिखित है:

static PriorityBlockingQueue<Runnable> queue=new PriorityBlockingQueue<Runnable>(MAXPOOL,new DownloadThreadComparator()); 

& &

class DownloadThreadComparator implements Comparator<Runnable>{ 

तो ThreadPoolExecutor स्वीकार करने के लिए एक BlockingQueue<? extends Runnable> तुम्हारे पास क्या है काम करेगा लिखा गया था।

+0

mDownload थ्रेड उपकरण रननेबल भी। तो यह सही समस्या नहीं होनी चाहिए? – Addev

+0

नहीं। जैसे आप 'सूची ' को 'सूची ' –

+0

में पास नहीं कर सकते हैं, यह तुलना करने के लिए तुलनात्मक रूप से भविष्य में कार्य के उदाहरणों को पास करता है। यह कानूनी है क्योंकि यह रननेबल से फैली हुई है। प्रश्न में वस्तु की तुलना के लिए आवश्यक विशेषताओं पर आप नहीं मिल सकते हैं। बट में असली दर्द। – Mike

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