优先级的ThreadPoolExecutor Java中(安卓) [英] Priority ThreadPoolExecutor in Java (Android)

查看:403
本文介绍了优先级的ThreadPoolExecutor Java中(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与优先级的ThreadPoolExecutor的。所以我定义了一个

 私有静态的ThreadPoolExecutor线程池=新的ThreadPoolExecutor(30,MAXPOOL,MAXPOOL,TimeUnit.SECONDS,
     队列,新mThreadFactory());
 

所以关键是队列现在参考。但是,当我宣布:

 静态的PriorityBlockingQueue< mDownloadThread>队列=新的PriorityBlockingQueue< mDownloadThread>(MAXPOOL,新DownloadThreadComparator());
 

编译器给出的第一行错误:构造的ThreadPoolExecutor(INT,INT,INT,为TimeUnit,的PriorityBlockingQueue,FileAccess.mThreadFactory)未定义与单一的quickfix:更改类型排队来的BlockingQueue的的。你能帮助我了解什么问题?

感谢

附加信息:

有关比较可运行我implementd以下类

 类mDownloadThread实现Runnable {
    私人的Runnable mRunnable;
    私人诠释优先;
    mDownloadThread(Runnable的可运行){
        mRunnable =可运行;
    }

    @覆盖
    公共无效的run(){
        mRunnable.run();
    }

    公众诠释getPriority(){
        返回优先;
    }

    公共无效setPriority(INT优先级){
        this.priority =优先;
    }
}
 

比较:

 类DownloadThreadComparator实现比较< mDownloadThread> {
    @覆盖
    公众诠释比较(mDownloadThread为arg0,mDownloadThread ARG1){

    如果(将arg0 == NULL和放大器;&安培; ARG1 == NULL){
        返回0;
    }否则,如果(为arg0 == NULL){
        返回-1;
    }否则,如果(ARG1 == NULL){
        返回1;
    }
    返回arg0.getPriority() -  arg1.getPriority();

    }

}
 

解决方案

的ThreadPoolExecutor 构造函数接受的BlockingQueue<可运行> 而不是的BlockingQueue&LT ;?扩展了Runnable> ,所以你不能传递给它的PriorityBlockingQueue< mDownloadThread> 实例

您可以更改队列类型的PriorityBlockingQueue<可运行> ,但在这种情况下,你会不会能够实施比较< mDownloadThread> 而无需进行转换在方法

另外一个解决办法是绕过泛型类型检查,但是这将是你的责任,提交 mDownloadThread 只实例执行方法:

 静态的ThreadPoolExecutor线程池=新的ThreadPoolExecutor(30,MAXPOOL,
        MAXPOOL,TimeUnit.SECONDS,(的PriorityBlockingQueue)队列,新mThreadFactory());
 

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

    }

}

解决方案

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

这篇关于优先级的ThreadPoolExecutor Java中(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆