`
liudaoru
  • 浏览: 1558041 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

多线程任务调度学习

    博客分类:
  • java
阅读更多

昨天找到一套多线程任务调度的代码,相当的不错,先把思路总结一下。

 

首先需要有一个任务管理器来管理所有的任务,任务管理器提供添加新任务的接口。

 

然后需要有一个线程池管理器管理所有的线程,线程分三种状态:创建、运行、空闲三种状态,线程可以执行任务Task。

 

主流程通过一个TaskMonitorThread 的任务调度线程来调度任务,方法就是每次从任务队列中获取一个任务,然后再从线程池中取一个线程来执行。

 

另外还有一个TaskTimeOutThread 的任务超时监控线程来监控任务是否超时,这个需要一个存储运行时间的类来支撑。

 

一个线程在开始执行时会触发开始运行的事件,在结束时会触发运行结束的事件,这些事件会将线程重新调整为空闲状态,扔回线程池。

 

学习的代码是:http://www.iteye.com/topic/487152

分享到:
评论
2 楼 yyh123456 2012-07-13  
不错,真有用阿,这为这事而来
1 楼 liudaoru 2010-06-23  
Amobe里的一个Queue类,贴出来学习一下。。。

/*

 * 	This program is free software; you can redistribute it and/or modify it under the terms of 

 * the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, 

 * or (at your option) any later version. 

 * 

 * 	This program is distributed in the hope that it will be useful, 

 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

 * See the GNU General Public License for more details. 

 * 	You should have received a copy of the GNU General Public License along with this program; 

 * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 */

package com.meidusa.amoeba.util;

/**
 * A queue implementation that is more efficient than a wrapper around
 * java.util.Vector. Allows adding and removing elements to/from the beginning,
 * without the unneccessary System.arraycopy overhead of java.util.Vector.
 */
public class Queue<T> {
	public Queue(int suggestedSize) {
		_size = _suggestedSize = suggestedSize;
		_items = newArray(_size);
	}

	public Queue() {
		this(4);
	}

	public synchronized void clear() {
		_count = _start = _end = 0;
		_size = _suggestedSize;
		_items = newArray(_size);
	}

	public synchronized boolean hasElements() {
		return (_count != 0);
	}

	public synchronized int size() {
		return _count;
	}

	public synchronized void prepend(T item) {
		if (_count == _size) {
			makeMoreRoom();
		}

		if (_start == 0) {
			_start = _size - 1;
		} else {
			_start--;
		}

		_items[_start] = item;
		_count++;

		if (_count == 1) {
			notify();
		}
	}

	/**
	 * Appends the supplied item to the end of the queue, and notify a consumer
	 * if and only if the queue was previously empty.
	 */
	public synchronized void append(T item) {
		// only notify if the queue was previously empty
		append0(item, _count == 0);
	}

	/**
	 * Appends an item to the queue without notifying anyone. Useful for
	 * appending a bunch of items and then waking up the listener.
	 */
	public synchronized void appendSilent(T item) {
		append0(item, false);
	}

	/**
	 * Appends an item to the queue and notify a listener regardless of how many
	 * items are on the queue. Use this for the last item you append to a queue
	 * in a batch via <code>appendSilent</code> because the regular
	 * <code>append</code> will think it doesn't need to notify anyone because
	 * the queue size isn't zero prior to this add. You should also use this
	 * method if you have mutiple consumers listening waiting on the queue, to
	 * guarantee that one will be woken for every element added.
	 */
	public synchronized void appendLoud(T item) {
		append0(item, true);
	}

	/**
	 * Internal append method. If subclassing queue, be sure to call this method
	 * from inside a synchronized block.
	 */
	protected void append0(T item, boolean notify) {
		if (_count == _size) {
			makeMoreRoom();
		}
		_items[_end] = item;
		_end = (_end + 1) % _size;
		_count++;

		if (notify) {
			notify();
		}
	}

	/**
	 * Returns the next item on the queue or null if the queue is empty. This
	 * method will not block waiting for an item to be added to the queue.
	 */
	public synchronized T getNonBlocking() {
		if (_count == 0) {
			return null;
		}

		// pull the object off, and clear our reference to it
		T retval = _items[_start];
		_items[_start] = null;

		_start = (_start + 1) % _size;
		_count--;

		return retval;
	}

	/**
	 * Blocks the current thread waiting for an item to be added to the queue.
	 * If the queue is currently non-empty, this function will return
	 * immediately.
	 */
	public synchronized void waitForItem() {
		while (_count == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
	}

	/**
	 * Gets the next item from the queue blocking for no longer than
	 * <code>maxwait</code> milliseconds waiting for an item to be added to
	 * the queue if it is empty at the time of invocation.
	 */
	public synchronized T get(long maxwait) {
		if (_count == 0) {
			try {
				wait(maxwait);
			} catch (InterruptedException e) {
			}

			// if count's still null when we pull out, we waited
			// ourmaxwait time.
			if (_count == 0) {
				return null;
			}
		}

		return get();
	}

	/**
	 * Gets the next item from the queue, blocking until an item is added to the
	 * queue if the queue is empty at time of invocation.
	 */
	public synchronized T get() {
		while (_count == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}

		// pull the object off, and clear our reference to it
		T retval = _items[_start];
		_items[_start] = null;

		_start = (_start + 1) % _size;
		_count--;

		// if we are only filling 1/8th of the space, shrink by half
		if ((_size > MIN_SHRINK_SIZE) && (_size > _suggestedSize)
				&& (_count < (_size >> 3)))
			shrink();

		return retval;
	}

	private void makeMoreRoom() {
		T[] items = newArray(_size * 2);
		System.arraycopy(_items, _start, items, 0, _size - _start);
		System.arraycopy(_items, 0, items, _size - _start, _end);
		_start = 0;
		_end = _size;
		_size *= 2;
		_items = items;
	}

	// shrink by half
	private void shrink() {
		T[] items = newArray(_size / 2);

		if (_start > _end) {
			// the data wraps around
			System.arraycopy(_items, _start, items, 0, _size - _start);
			System.arraycopy(_items, 0, items, _size - _start, _end + 1);

		} else {
			// the data does not wrap around
			System.arraycopy(_items, _start, items, 0, _end - _start + 1);
		}

		_size = _size / 2;
		_start = 0;
		_end = _count;
		_items = items;
	}

	@SuppressWarnings("unchecked")
	private T[] newArray(int size) {
		return (T[]) new Object[size];
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();

		buf.append("[count=").append(_count);
		buf.append(", size=").append(_size);
		buf.append(", start=").append(_start);
		buf.append(", end=").append(_end);
		buf.append(", elements={");

		for (int i = 0; i < _count; i++) {
			int pos = (i + _start) % _size;
			if (i > 0)
				buf.append(", ");
			buf.append(_items[pos]);
		}

		return buf.append("}]").toString();
	}

	protected final static int MIN_SHRINK_SIZE = 1024;

	protected T[] _items;
	protected int _count = 0;
	protected int _start = 0, _end = 0;
	protected int _suggestedSize, _size = 0;
}

相关推荐

    Java开源的分布式任务调度平台 xxl-job.zip

    XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 1.2 特性 1、简单:支持通过Web页面对任务进行CRUD操作,...

    操作系统——线程

    学习操作系统时,需要做多线程的实验,这是我找的一些资料,对我非常有帮助,希望也能对你有帮助。。。。。。

    Python—-多线程—-多任务

    并发:指的是任务数多于cpu核数,通过操作系统的各种任务调度算法,实现用多个任务“一起”执行(实际上总有一些任务不在执行,因为切换任务的速度相当快,看上去一起执行而已) 并行:指的是任务数小于等于cpu核数...

    Java程序设计案例教程-第8章-多线程编程.pptx

    第8章 多线程编程 第1页 本章概述 本章的学习目标 主要内容 Java程序设计案例教程-第8章-多线程编程全文共36页,当前为第1页。 本章概述 前面我们所开发的程序大多是单线程的,即一个程序只有一条从头到尾的执行路线...

    XXL-JOB调度系统学习、调度流程、spring生命周期

    XXL-JOB学习 xxl-job主流分析,包含调度客户端任务自动注册,服务端内部调度线程调度分析 开源源码赏析,代码分析,spring启动流程分析 Java多线程,线程池启停,设计模式分析

    【十三】Java多线程(指尖上的多线程[超详细])

    多线程特别重要,虽然内容偏多,但是需要熟练掌握。面试也会在此章节有考验的!请大家耐心学习! 目录 一、什么是线程 二、线程的组成(创建线程方式) 三、线程的状态(方法实例详解) 四、线程安全(实例详解) ...

    第7章-JUC多线程v1.1.pdf

    JAVA线程基本学习, JAVA多线程的特性= 线程池: 本质上是一个对象池, 用来管理线程资源. 在任务执行前, 需要从线程池中拿出线程来执行. 在任务执行完成之后, 需要把线程放回线程池. 线程池好处: 降低资源的消耗...

    java简易投票系统源码下载-StudyThread:学习多线程记录

    简介:个人学习多线程所记录; 1.Java多线程技能 1.1 进程和多线程的概念及线程的优点? 讲到多线程技术时,就不得不提及"进程"这个概念了。百度对进程的接受如下: 进程:是操作系统的基础,是一次程序的执行,是一...

    rt thread自学笔记

    RT-Thread,全称是 Real Time-Thread,顾名思义,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行并不意味着处理器在同一时刻真地执行了多个任务。事实上,一个处理器核心在某一...

    Python任务调度模块APScheduler使用

    主要介绍了Python任务调度模块APScheduler使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    生产者-消费者模型模拟进程调度,带报告,课程设计

    2.了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。 3.学习使用Windows 2000/XP中基本的同步对象,掌握相应的API。 三、实验要求 1.生产者消费者对缓冲区进行互斥操作。 2.缓冲区大小为10,缓冲区...

    xxl-job分布式任务调度平台-其他

    XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 XXL-JOB特点: 1、简单:支持通过Web页面对任务进行CRUD操作,...

    多线程学习篇之线程池基础

    多线程处理就是允许一个进程中在同一时刻执行多个任务。 1.2 多线程创建方式 1.2.1.继承Thread类 下面展示一些 内联代码片。 /** * 使用继承Thread 方式创建线程池 * @作者 陈晨辉 * @创建日期 2020/3/18 15:

    JobFlow 定时任务管理系统, 使用Go语言开发的轻量级定时任务集中调度和管理系统

    Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...

    Job Plus项目是基于SpringBoot+Vue的轻量级定时任务管理系统+源代码+文档说明

    26. 多线并发:系统支持多线程触发调度运行,确保调度精确执行,不被堵塞; 27. 降级隔离:调度线程池进行隔离拆分,慢任务自动降级进入"Slow"线程池,避免耗尽调度线程,提高系统稳定性; 28. Gradle: 将会把最新...

    大家应该掌握的多线程编程

    不管你是初学者,还是资深的老司机,多线程是在学习,面试和工作中都要经常被提及的一个话题,下面我们就来看一看具体的相关内容。 1、多线程编程必备知识 1.1 进程与线程的概念 当我们打开一个应用程序后,操作系统...

    juc:Java多线程学习笔记源码部分

    多进程的方式也可以实现并发,为什么我们要使用多线程? 共享资源在线程间的通信比较容易。 线程开销更小。 进程和线程的区别? 进程是一个独立的运行环境,而线程是在进程中执行的一个任务。他们两个本质的区别是...

    2023最新springboot面试题

    内容概要:最新2023年springboot后端面试题整理, 包含Actuator,JPA,运维管理,单元测试,任务调度,配置等等问题, 用简洁明了的语言,通俗易懂地阐述了高并发多线程相关面试的知识点。 适用人群:适合想了解或...

    Linux系统设计-高可用分布式调度系统, 支持定时调度(类似linux crontab)和依赖调度.zip

    Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...

Global site tag (gtag.js) - Google Analytics