- 浏览: 1558041 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
nich002:
原网站失效了。撸主简单粗暴的复制过来,可读性极差!差评!
Apache配置详解(最好的APACHE配置教程) -
107x:
不错,谢谢!
LINUX下查看文件夹下的文件个数! -
Hypereo:
好你妹,连个格式都没有!
Apache配置详解(最好的APACHE配置教程) -
resteater:
代码排版感觉有点乱!收发信息代码可读性不强!请问第一次发服务器 ...
java socket例子 -
resteater:
代码排版感觉有点乱!收发信息代码可读性不强!请问第一次发服务器 ...
java socket例子
昨天找到一套多线程任务调度的代码,相当的不错,先把思路总结一下。
首先需要有一个任务管理器来管理所有的任务,任务管理器提供添加新任务的接口。
然后需要有一个线程池管理器管理所有的线程,线程分三种状态:创建、运行、空闲三种状态,线程可以执行任务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; }
发表评论
-
JVM问题追查与调优
2012-03-27 14:44 1106JDK的几种分析工具 http://liudaoru ... -
NodeJs相关资料
2011-08-18 14:55 2937NodeJs获取参数: proces ... -
jprofiler追踪问题
2011-08-12 18:20 1007Jprofiler下载页: http://www.ej ... -
Linux服务器性能评估与优化【z】
2011-07-01 10:05 1508来自:http://www.itlearner.com/ ... -
Java 理论与实践: 非阻塞算法简介【z】
2011-03-26 20:39 1239From: http://www.ibm.com/develo ... -
Java Crash问题分析[z]
2011-03-23 14:41 5930参考: http://www.ibm.com/develop ... -
Berkeley DB相关
2010-09-25 22:17 1022为什么要使用Berkeley DB,它适合什么场合应用?Ber ... -
熟悉系统方法总结
2010-07-06 14:26 783了解一个陌生的系统是我们经常碰到的事情,下面总结一下自己的一些 ... -
Java缓存框架 EhCache
2010-07-06 14:09 4689From: http://www.oschina.net/p/ ... -
【nio】使用 ServerSocketChannel 实现的 File 服务器[z]
2010-05-21 17:31 3918From: http://www.java2000.net/p ... -
Memcached命令行管理
2010-03-15 11:18 4454From: http://www.exp2up.com/2 ... -
(转)Resin服务器配置指南
2010-01-21 15:35 3412From:http://blog.21cn.com/super ... -
Flickr架构
2010-01-11 09:52 1240From: http://www.cyask.com/ques ... -
JDK的几种分析工具
2009-12-04 12:13 10870From: http://blog.csdn.net/hant ... -
XMemcached——一个新的开源Java memcached客户端
2009-10-23 09:27 1856From: http://www.infoq.com/cn/ ... -
用HSCALE实现MySQL的数据分布式存储
2009-10-15 12:47 2975From:http://www.ningoo.net/ht ... -
马化腾:搜索、电子商务硬仗一定要坚持打
2009-10-15 12:09 1688From:http://www.techweb.com.c ... -
MySQL分表实现上百万上千万记录分布存储的批量查询设计模式【z】
2009-10-15 09:56 3137From:http://hi.baidu.com/jabber ... -
nginx负载均衡和lvs负载均衡的比较分析【z】
2009-10-13 20:02 1427From:http://www.shouker.com/u ... -
新型的大型bbs架构(squid+nginx)【z】
2009-10-13 19:53 1582From:http://www.fovweb.com/opti ...
相关推荐
XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 1.2 特性 1、简单:支持通过Web页面对任务进行CRUD操作,...
学习操作系统时,需要做多线程的实验,这是我找的一些资料,对我非常有帮助,希望也能对你有帮助。。。。。。
并发:指的是任务数多于cpu核数,通过操作系统的各种任务调度算法,实现用多个任务“一起”执行(实际上总有一些任务不在执行,因为切换任务的速度相当快,看上去一起执行而已) 并行:指的是任务数小于等于cpu核数...
第8章 多线程编程 第1页 本章概述 本章的学习目标 主要内容 Java程序设计案例教程-第8章-多线程编程全文共36页,当前为第1页。 本章概述 前面我们所开发的程序大多是单线程的,即一个程序只有一条从头到尾的执行路线...
XXL-JOB学习 xxl-job主流分析,包含调度客户端任务自动注册,服务端内部调度线程调度分析 开源源码赏析,代码分析,spring启动流程分析 Java多线程,线程池启停,设计模式分析
多线程特别重要,虽然内容偏多,但是需要熟练掌握。面试也会在此章节有考验的!请大家耐心学习! 目录 一、什么是线程 二、线程的组成(创建线程方式) 三、线程的状态(方法实例详解) 四、线程安全(实例详解) ...
JAVA线程基本学习, JAVA多线程的特性= 线程池: 本质上是一个对象池, 用来管理线程资源. 在任务执行前, 需要从线程池中拿出线程来执行. 在任务执行完成之后, 需要把线程放回线程池. 线程池好处: 降低资源的消耗...
简介:个人学习多线程所记录; 1.Java多线程技能 1.1 进程和多线程的概念及线程的优点? 讲到多线程技术时,就不得不提及"进程"这个概念了。百度对进程的接受如下: 进程:是操作系统的基础,是一次程序的执行,是一...
RT-Thread,全称是 Real Time-Thread,顾名思义,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行并不意味着处理器在同一时刻真地执行了多个任务。事实上,一个处理器核心在某一...
主要介绍了Python任务调度模块APScheduler使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
2.了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。 3.学习使用Windows 2000/XP中基本的同步对象,掌握相应的API。 三、实验要求 1.生产者消费者对缓冲区进行互斥操作。 2.缓冲区大小为10,缓冲区...
XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 XXL-JOB特点: 1、简单:支持通过Web页面对任务进行CRUD操作,...
多线程处理就是允许一个进程中在同一时刻执行多个任务。 1.2 多线程创建方式 1.2.1.继承Thread类 下面展示一些 内联代码片。 /** * 使用继承Thread 方式创建线程池 * @作者 陈晨辉 * @创建日期 2020/3/18 15:
Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...
26. 多线并发:系统支持多线程触发调度运行,确保调度精确执行,不被堵塞; 27. 降级隔离:调度线程池进行隔离拆分,慢任务自动降级进入"Slow"线程池,避免耗尽调度线程,提高系统稳定性; 28. Gradle: 将会把最新...
不管你是初学者,还是资深的老司机,多线程是在学习,面试和工作中都要经常被提及的一个话题,下面我们就来看一看具体的相关内容。 1、多线程编程必备知识 1.1 进程与线程的概念 当我们打开一个应用程序后,操作系统...
多进程的方式也可以实现并发,为什么我们要使用多线程? 共享资源在线程间的通信比较容易。 线程开销更小。 进程和线程的区别? 进程是一个独立的运行环境,而线程是在进程中执行的一个任务。他们两个本质的区别是...
内容概要:最新2023年springboot后端面试题整理, 包含Actuator,JPA,运维管理,单元测试,任务调度,配置等等问题, 用简洁明了的语言,通俗易懂地阐述了高并发多线程相关面试的知识点。 适用人群:适合想了解或...
Linux系统是一个免费使用和自由传播的类Unix操作系统,基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统,Linux是许多企业...