【发布时间】:2019-12-29 19:10:56
【问题描述】:
这有什么区别?请参阅选项 1 和选项 2。因为我遇到了麻烦,因为它们好像是一样的。它们运行正确
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
Thread callLogsThread = new Thread(new CallLogsRunnable());
Thread contactsThread = new Thread(new ContactsRunnable());
/*option 1**/
executor.submit(new ContactsRunnable());
executor.submit(new CallLogsRunnable());
/*option 2**/
executor.submit(contactsThread);
executor.submit(callLogsThread);
/*option 3**/
contactsThread.start();
contactsThread.join();
callLogsThread.start();
callLogsThread.join();
executor.shutdown();
更新:我添加了选项 3。现在选项 1 和选项 3 之间有什么区别。因为我知道选项 2 是废话。谢谢
【问题讨论】:
-
Thread implements Runnable,这通常被认为是一个错误。使用Thread#run(),就像你的执行程序在这里所做的那样,除了在内存中有额外的对象外,与直接调用runnable没有什么不同。
标签: java android multithreading threadpool