1.直接使用線程池:
對(duì)線程池不熟悉的可以看我另外一篇博客。上一篇的代碼這一篇也有用到。線程池簡(jiǎn)介
/**
* 串行執(zhí)行多線程任務(wù) 第一種 使用線程池
*/
//任務(wù)隊(duì)列瓮栗。android 提供有:LinkedBlockingQueue ArrayQueue ArrayBlockQueue 區(qū)別自己百度吧。
LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(100);
//線程工廠
ThreadFactory threadFactory = new ThreadFactory(){
@Override
public Thread newThread(@NonNull Runnable r) {
return new Thread(r);
}
};
//設(shè)置一個(gè)核心線程數(shù)為1瞄勾,最大線程數(shù)為5遵馆,任務(wù)隊(duì)列最大容量為100,閑置關(guān)閉時(shí)間為1秒的線程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 5, 1l, java.util.concurrent.TimeUnit.SECONDS, workQueue, threadFactory);
public void executeByExecutor(Runnable runnable){
threadPoolExecutor.execute(runnable);
}
<img src="https://upload-images.jianshu.io/upload_images/13400445-50bccacbeba69c7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="500" height="313" align=center/>
Screenshot_2018-09-14-16-47-03.png
任務(wù)升級(jí):我有時(shí)想串行丰榴,有時(shí)想并行货邓。
沒(méi)必要?jiǎng)?chuàng)建兩個(gè)線程池。我們直接用一個(gè)線程池就能實(shí)現(xiàn)四濒。
public class MyQueueAndThreadPool {
private final ThreadPoolExecutor threadPoolExecutor;
private ArrayDeque<Runnable> mTasks;
private Runnable mActive;
public MyQueueAndThreadPool(){
mTasks = new ArrayDeque<>();
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(@NonNull Runnable r) {
return new Thread(r);
}
};
;
threadPoolExecutor = new ThreadPoolExecutor(5, 10, 60, java.util.concurrent.TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(), threadFactory);
}
public void execute(final Runnable r){
mTasks.offer(new Runnable() {
@Override
public void run() {
r.run();
doOnNext();
}
});
//第一個(gè)進(jìn)去换况,啟動(dòng)這個(gè)循環(huán)职辨。
if(mActive == null){
doOnNext();
}
}
public void executeOnExecutor(Runnable r){
threadPoolExecutor.execute(r);
}
private void doOnNext(){
if((mActive = mTasks.poll())!=null){
threadPoolExecutor.execute(mActive);
};
}
}
//執(zhí)行。
// executeByExecutor(runnable);
// executeByExecutor(runnable_2);
// executeByExecutor(runnable_3);
myQueueAndThreadPool.executeOnExecutor(runnable);
myQueueAndThreadPool.executeOnExecutor(runnable_2);
myQueueAndThreadPool.executeOnExecutor(runnable_3);
[圖片上傳中...(Screenshot_2018-09-17-10-55-12.png-b6988c-1537153273421-0)]
Screenshot_2018-09-17-10-55-12.png
其實(shí)這里就是asyncTask的內(nèi)部邏輯戈二。只是asyncTask同時(shí)考慮了異常和線程同步的問(wèn)題舒裤,我這里只是簡(jiǎn)單實(shí)現(xiàn)了一下原理。觉吭。腾供。
3.使用wait和notify
public class ThreadSerialzetow {
public static void main(String[] args){
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB();
ThreadC threadC = new ThreadC();
threadA.add(threadC);
threadB.add(threadA);
threadC.add(threadB);
threadA.start();
threadB.start();
threadC.start();
}
static class ThreadA extends Thread{
public ThreadC threadc;
@Override
public void run() {
while (true){
synchronized(threadc){
synchronized(this){
System.out.println("我是A");
this.notify();
}
try {
threadc.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void add(ThreadC th){
threadc = th;
}
}
static class ThreadB extends Thread{
public ThreadA threada;
@Override
public void run() {
while(true){
synchronized(threada){
synchronized(this){
System.out.println("我是B");
this.notify();
}
try {
threada.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void add(ThreadA th){
threada = th;
}
}
static class ThreadC extends Thread{
public ThreadB threadb;
@Override
public void run() {
while(true){
synchronized(threadb){
synchronized(this){
System.out.println("我是C");
this.notify();
}
try {
threadb.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void add(ThreadB th){
threadb = th;
}
}
}
這段代碼不是我寫(xiě)的。是在網(wǎng)上找的鲜滩。運(yùn)行起來(lái)可以實(shí)現(xiàn)a>b>c順序無(wú)限執(zhí)行伴鳖,有時(shí)可能會(huì)死鎖,有時(shí)會(huì)出現(xiàn)a>c>b然后轉(zhuǎn)為a>b>c徙硅。其中玄妙自己體會(huì)吧榜聂。。