Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task问题请教 #9

Closed
brucewuu520 opened this issue Jul 17, 2015 · 2 comments
Closed

Task问题请教 #9

brucewuu520 opened this issue Jul 17, 2015 · 2 comments

Comments

@brucewuu520
Copy link

在Android操作系统中,一般会更具cpu的核心数来定义并发线程的数量,所以TaskQueue如果使用自动以的线程池可否这样设置:TaskQueue.getDefault().setExecutor(...);
看文档上写的是TaskQueue.createNew().setExecutor(...);这样TaskQueue就不是单例了;
我想请教作者,如果我想用自定义的线程池而且想用单例那么这么设置TaskQueue.getDefault().setExecutor(...)不会有错吧?

@mcxiaoke
Copy link
Owner

@brucewuu520

  1. TaskQueue.getDefault().setExecutor() 这样是可以的,修改的是默认TaskQueue的设置
  2. 如果使用TaskQueue.createNew()就等于是创建了一个新的TaskQueue,你在某个地方保存这个TaskQueue,整个App只使用这个实例也等于是单例,一般都是保存在Application的子类中,

两种用法是等价的,如果整个应用都只用一个TaskQueue,那么随便选一种都可以

例子:

public class MyApp extends Application {
    private TaskQueue mQueue;

    @Override
    public void onCreate() {
        super.onCreate();
        mQueue = TaskQueue.createNew();
        mQueue.setExecutor(Executors.newFixedThreadPool(2));
    }

    public TaskQueue getQueue() {
        return mQueue;
    }
}
// 对于情况一
TaskQueue queue=TaskQueue.getDefault();
// 对于情况二
MyApp app=(MyApp) context.getApplicationContext();
TaskQueue queue=app.getQueue();

@mcxiaoke
Copy link
Owner

close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants