wait

 

 

// Processor.java
package money;

import java.util.ArrayList;
import java.util.List;

import money.thread.ExecutableRunner;

public class Processor extends Thread {

    private static final String TAG = "Processor";

    private List<ExecutableRunner> mReadyTasks;
    private List<ExecutableRunner> mExecutingTasks;

    private boolean running;
    private Object readyTaskListLock;

    // TODO:
    private boolean concurrent = false;
    private static Processor processor;

    public synchronized static Processor instance() {
        if (processor == null) {
            processor = new Processor();
        }

        return processor;
    }

    private Processor() {
        mReadyTasks = new ArrayList<ExecutableRunner>();
        mExecutingTasks = new ArrayList<ExecutableRunner>();
        running = true;
        readyTaskListLock = new Object();
    }

    public Object getReadyTaskListLock() {
        return readyTaskListLock;
    }
    
    
    @Override
    public void run() {
        Log.i(TAG, "Processor begin...");

        while (running) {
            int taskSize;
            Log.i(TAG, "to while:");
            taskSize = mReadyTasks.size();
            Log.d(TAG, "taskSize:" + taskSize);
            
            //TODO:check the system is busy?
            if (taskSize > 0) {
                runTask();
            }

            synchronized (readyTaskListLock) {
                try {
                    readyTaskListLock.wait(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void runTask() {
        Log.d(TAG, "runTask()");
        ExecutableRunner runner;
        if (mReadyTasks.size() > 0) {
            synchronized (readyTaskListLock) {

                runner = mReadyTasks.remove(0);
                if (runner == null) {
                    Log.e(TAG, "runner == null");
                }
            }

            new Thread(runner).start();
            mExecutingTasks.add(runner);
        }
    }

    public boolean addTask(ExecutableRunner runner) {
        synchronized (readyTaskListLock) {
            mReadyTasks.add(runner);
            Log.d(TAG, "readyTaskListLock.notifyAll()");
            readyTaskListLock.notifyAll();
        }

        return true;
    }
}

 

package money.thread;

public class ExecutableRunner implements Runnable {
    
    public static final int RUN_TYPE_DEFAULT = 0;
    
    public static final int RUN_TYPE_MAIN_THREAD = 1;
    

    protected Object readyTaskListLock;
    protected volatile boolean stop;
    
    private int type;
    private int exclusiveValue;
    protected String description;

    public ExecutableRunner(Object readyTaskListLock, String description, int exclusiveValue, int type) {
        this.readyTaskListLock = readyTaskListLock;
        this.description = description;
        this.exclusiveValue = exclusiveValue;
        this.type = type;
        stop = false;
    }    
    
    public int getType() {
        return type;
    }
    
    public int getExclusiveValue() {
        return exclusiveValue;
    }
    
    public String getDescription() {
        return description;
    }

    public void stopRunning() {
        stop = true;
    }

    @Override
    public void run() {
      
    }
}

 

posted @ 2017-10-26 10:39  牧 天  阅读(142)  评论(0)    收藏  举报