taskmanagerimplementation.java

来自「ajax patterns 这是关于ajax设计模式方面的原代码」· Java 代码 · 共 115 行

JAVA
115
字号
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package devspace.ajax.infinitedata;
import java.util.*;
import devspace.testDrivenDevelopment.tracer.*;

public class TaskManagerImplementation implements TaskManager, Runnable {
    private Queue<Task> _tasks = new LinkedList< Task>();
    private Queue<Result> _results = new LinkedList< Result>();
    private Thread _thread = null;
    private int _taskCount;
    private Object _objTaskCount = new Object();

    public TaskManagerImplementation() {
    }
    
    private synchronized Task getTask() {
        Task task = null;
        if (_tasks.size() > 0) {
            task = _tasks.poll();
        }
        return task;
    }
    public void run() {
        Task task = null;
        while (true) {
            task = getTask();
            if (task != null) {
                task.execute(this);
                synchronized( _objTaskCount) {
                    _taskCount --;
                }
            }
            else {
                synchronized(this) {
                    task = getTask();
                    if (task == null) {
                        _thread = null;
                        break;
                    }
                }
            }
        }
    }
    public synchronized void runThreadedTasks() {
        if (_thread == null) {
            _thread = new Thread( this);
            _thread.start();
        }
    }
    public synchronized void addTask(Task task) {
        _tasks.add(task);
    }
    
    public boolean anyOutstandingTasks() {
        synchronized( _objTaskCount) {
            return _taskCount != 0;
        }
    }
    
    public void addResult(Result result) {
        GenerateOutput.Write("TaskManagerImpl.AddResult", "Generated result (" + result.toString() + ")");
        synchronized( _results) {
            _results.add( result);
            _results.notify();
            synchronized( _objTaskCount) {
                _taskCount ++;
            }
        }
    }
    private Result getSingleResult() {
        Result result = null;
        if (_results.size() > 0) {
            result = _results.poll();
            GenerateOutput.Write("TaskManagerImpl.GetSingleResult", "Retrieving result (" + result.toString() + ")");
        }
        return result;
    }
    public Result getResult() {
        Result result = null;
        synchronized( _results) {
            result = getSingleResult();
        }
        return result;
    }
    public Result getResultWait(long timeout) {
        Result result = null;
        synchronized( _results) {
            result = getSingleResult();
            if (result == null) {
                try {
                    _results.wait(timeout * 1000);
                }
                catch (InterruptedException e) {}
                result = getSingleResult();
            }
        }
        return result;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?