Java 中的优先级 ThreadPoolExecutor (Android)

发布于 2024-12-10 00:22:49 字数 1559 浏览 1 评论 0原文

我正在尝试创建一个具有优先级的 ThreadPoolExecutor 。所以我定义了一个

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

所以现在关键是队列引用。但是当我声明时:

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

编译器在第一行给出错误:构造函数 ThreadPoolExecutor(int, int, int, TimeUnit, PriorityBlockingQueue, FileAccess.mThreadFactory) 未定义,并使用一个快速修复:将“队列”类型更改为 BlockingQueue。你能帮我了解问题所在吗?

谢谢

附加信息:

为了比较可运行对象,我实现了以下类

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

比较器:

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

    }

}

I'm trying to make an ThreadPoolExecutor with priority. So I define a

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

So the key is the queue reference now. But when I declare :

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

The compiler gives an error in the first line: The constructor ThreadPoolExecutor(int, int, int, TimeUnit, PriorityBlockingQueue, FileAccess.mThreadFactory) is undefined with a single quickfix: Change type of 'queue' to BlockingQueue. Can you help me to understand whats the problem?

Thanks

Additional Info:

For comparing the runnables I implementd the following class

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

The comparator:

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2024-12-17 00:22:49

ThreadPoolExecutor 构造函数接受 BlockingQueue 而不是 BlockingQueue,因此您无法将其传递给 PriorityBlockingQueue 实例。

您可以将 queue 类型更改为 PriorityBlockingQueue,但在这种情况下,您将无法实现 Comparatorcompare 方法内进行转换。

另一种解决方案是绕过通用类型检查,但您有责任仅将 mDownloadThread 实例提交给 execute 方法:

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

ThreadPoolExecutor constructor accepts BlockingQueue<Runnable> and not BlockingQueue<? extends Runnable>, thus you can't pass to it PriorityBlockingQueue<mDownloadThread> instance.

You can change type of queue to PriorityBlockingQueue<Runnable>, but in that case you won't be able to implement Comparator<mDownloadThread> without casting inside the compare method.

The other solution is to bypass generic type-checking but it would be your responsibility to submit only instances of mDownloadThread to execute method:

static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, 
        MAXPOOL, TimeUnit.SECONDS, (PriorityBlockingQueue) queue, new mThreadFactory());
惟欲睡 2024-12-17 00:22:49

问题是 ThreadPoolExecutor 需要一个 BlockingQueue 而您正在传递一个 PriorityBlockingQueuePriorityBlockingQueue 实现了 BlockingQueue,因此这不是问题。问题在于Runnable。您无法在需要 Runnable 的地方传递通用类型的 mDownloadThread

修复如下:

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

&&

class DownloadThreadComparator implements Comparator<Runnable>{  

如果编写ThreadPoolExecutor来接受BlockingQueue<?扩展 Runnable> 你所拥有的可以工作。

The problem is that ThreadPoolExecutor is expecting a BlockingQueue<Runnable> and you are passing a PriorityBlockingQueue<mDownloadThread>. The PriorityBlockingQueue implements BlockingQueue so that is not the problem. The problem is the Runnable. You cannot pass a generic type of mDownloadThread where Runnable was expected.

Fix is the following:

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

&&

class DownloadThreadComparator implements Comparator<Runnable>{  

If ThreadPoolExecutor were written to accept a BlockingQueue<? extends Runnable> what you have would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文