2011-10-17 26 views
6

Tôi đang cố gắng tạo một ThreadPoolExecutor với mức độ ưu tiên. Vì vậy, tôi xác định một sốChủ đề ưu tiênPoolExecutor trong Java (Android)

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

Vì vậy, khóa là tham chiếu hàng đợi ngay bây giờ. Nhưng khi tôi tuyên bố:

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

Trình biên dịch đưa ra một lỗi trong dòng đầu tiên: Các constructor ThreadPoolExecutor (int, int, int, TIMEUNIT, PriorityBlockingQueue, FileAccess.mThreadFactory) là undefined với một QuickFix duy nhất: Thay đổi loại 'hàng đợi' thành BlockingQueue. Bạn có thể giúp tôi hiểu vấn đề này không?

Cảm ơn

Thông tin bổ sung:

Đối với so sánh Runnables tôi implementd lớp sau

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; 
    }  
} 

Các so sánh:

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(); 

    } 

} 

Trả lời

6

ThreadPoolExecutor constructor chấp nhận BlockingQueue<Runnable> và không BlockingQueue<? extends Runnable>, do đó bạn không thể chuyển cho ví dụ PriorityBlockingQueue<mDownloadThread>.

Bạn có thể thay đổi loại queue thành PriorityBlockingQueue<Runnable>, nhưng trong trường hợp đó bạn sẽ không thể triển khai Comparator<mDownloadThread> mà không cần truyền bên trong phương thức compare.

Các giải pháp khác là để bỏ qua generic loại kiểm tra nhưng nó sẽ là trách nhiệm của bạn để nộp chỉ trường hợp của mDownloadThread-execute phương pháp:

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

Vấn đề là ThreadPoolExecutor đang mong đợi một BlockingQueue<Runnable> và bạn đang đi qua a PriorityBlockingQueue<mDownloadThread>. PriorityBlockingQueue thực hiện BlockingQueue để không phải là vấn đề. Vấn đề là Runnable. Bạn không thể chuyển loại chung của mDownloadThread trong đó Runnable được mong đợi.

Fix như sau:

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

& &

class DownloadThreadComparator implements Comparator<Runnable>{ 

Nếu ThreadPoolExecutor được viết để chấp nhận một BlockingQueue<? extends Runnable> những gì bạn có thể làm việc.

+0

mDownloadThread thực hiện cũng có thể chạy. vì vậy nó không phải là một vấn đề phải không? – Addev

+0

Không. Giống như bạn không thể chuyển 'Danh sách ' vào một 'Danh sách ' –

+0

Làm điều này chuyển các phiên bản của FutureTask vào Trình so sánh. Điều này là hợp pháp vì nó kéo dài từ Runnable. Tuy nhiên, bạn không thể nhận được các thuộc tính cần thiết để so sánh đối tượng được đề cập. Thực sự đau ở mông. – Mike