wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.lechange.demo.tools;
 
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.LockSupport;
 
public class TaskPoolHelper {
    private final static String TAG = "lcop_TaskPoolHelper";
    
    /**
     * 构造具备过滤的task
     */
    public static abstract class RunnableTask implements Runnable {
        public String mTaskID;
        
        public RunnableTask(String taskID){
            this.mTaskID = taskID;
        }
    }
    
    //private static ExecutorService mPool = Executors.newFixedThreadPool(3);
    //队列属性的task
    private static ArrayBlockingQueue<RunnableTask> mQueueTask = new ArrayBlockingQueue<RunnableTask>(50);
    private static List<String> mFilteTask = new ArrayList<String>();
    private static Thread mQueueThread;
    
    //实时属性的task
    private static RunnableTask mRealTask; //=null
    private static Thread mRealThread;
    
    static {
        mQueueThread = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                while (true) {    
                    try {
                        //自带阻塞光环
                        RunnableTask task = mQueueTask.take();
                        task.run();
                        mFilteTask.remove(task.mTaskID);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        mQueueThread.start();
        
        mRealThread = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                while (true) {
                    if (mRealTask == null) {
                        LockSupport.park();
                    } else {
                        RunnableTask task = mRealTask;
                        mRealTask = null;
                        task.run();
                    }                
                }
            }
        };
        mRealThread.start();
    }
    
    
    public static void addTask(RunnableTask task) {
        //过滤
        if (task.mTaskID.equals("real")) {
            mRealTask = task;
            LockSupport.unpark(mRealThread);
            return;
        }
        if (mFilteTask.contains(task.mTaskID)) {
            return;
        }
        
        try {
            mQueueTask.add(task);
            mFilteTask.add(task.mTaskID);
        } catch(IllegalStateException e) {
            LogUtil.debugLog(TAG, e.getMessage());
            mQueueTask.clear();
            mFilteTask.clear();
        }
    }
 
    
    
    public static void clearTask(){
        mQueueTask.clear();
        mFilteTask.clear();
    }
    
}