当前位置:首页 » 编程语言 » java队列实现

java队列实现

发布时间: 2022-06-09 02:33:01

‘壹’ java中的队列用什么实现

队列的实现单纯的是数据结构的问题,既可以用链表结构实现队列,也可以用数组实现。这和语言不是紧密关系,java可以这样实现,C、C++ 也可以。

‘贰’ java 用什么实现 FIFO队列

java使用数据结构来实现FIFO先进先出的队列,实例如下:

/*
*Tochangethistemplate,chooseTools|Templates
*andopenthetemplateintheeditor.
*/
packagelinkedlisttest;

importjava.util.ArrayList;
importjava.util.Deque;
importjava.util.LinkedList;
importjava.util.List;

/**
*
*@authorVicky.H
*@[email protected]
*/
publicclassFIFOTest{

/**
*@
*/
publicstaticvoidmain(String[]args){
FIFO<A>fifo=newFIFOImpl<A>(5);
for(inti=0;i<20;i++){
Aa=newA("A:"+i);
Ahead=fifo.addLastSafe(a);
System.out.println(i+" head:"+head+" size:"+fifo.size());
}

System.out.println("---------------");

System.out.println("弹出数据");
List<A>polls=fifo.setMaxSize(3);
for(Aa:polls){
System.out.println(" head:"+a);
}

System.out.println("剩余数据");
for(Aa:fifo){
System.out.println(" head:"+a);
}
System.out.println(fifo.size());
}
}

interfaceFIFO<T>extendsList<T>,Deque<T>,Cloneable,java.io.Serializable{

/**
*向最后添加一个新的,如果长度超过允许的最大值,则弹出一个*
*/
TaddLastSafe(TaddLast);

/**
*弹出head,如果Size=0返回null。而不同于pop抛出异常
*@return
*/
TpollSafe();

/**
*获得最大保存
*
*@return
*/
intgetMaxSize();

/**
*设置最大存储范围
*
*@return返回的是,因为改变了队列大小,导致弹出的head
*/
List<T>setMaxSize(intmaxSize);

}

classFIFOImpl<T>extendsLinkedList<T>implementsFIFO<T>{

privateintmaxSize=Integer.MAX_VALUE;
privatefinalObjectsynObj=newObject();

publicFIFOImpl(){
super();
}

publicFIFOImpl(intmaxSize){
super();
this.maxSize=maxSize;
}

@Override
publicTaddLastSafe(TaddLast){
synchronized(synObj){
Thead=null;
while(size()>=maxSize){
head=poll();
}
addLast(addLast);
returnhead;
}
}

@Override
publicTpollSafe(){
synchronized(synObj){
returnpoll();
}
}

@Override
publicList<T>setMaxSize(intmaxSize){
List<T>list=null;
if(maxSize<this.maxSize){
list=newArrayList<T>();
synchronized(synObj){
while(size()>maxSize){
list.add(poll());
}
}
}
this.maxSize=maxSize;
returnlist;
}

@Override
publicintgetMaxSize(){
returnthis.maxSize;
}
}

classA{

privateStringname;

publicA(){
}

publicA(Stringname){
this.name=name;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

@Override
publicStringtoString(){
return"A{"+"name="+name+'}';
}
}

‘叁’ 到底什么是消息队列Java中如何实现消息队列

“消息队列”是在消息的传输过程中保存消息的容器。和我们学过的LinkedHashMap,TreeSet等一样,都是容器。既然是容器,就有有自己的特性,就像LinkedHashMap是以键值对存储。存取顺序不变。而消息队列,看到队列就可以知道。这个容器里面的消息是站好队的,一般遵从先进先出原则。

java中已经为我们封装好了很多的消息队列。在java 1.5版本时推出的java.util.concurrent中有很多现成的队列供我们使用。特性繁多,种类齐全。是你居家旅游开发必备QAQ。

下面简单列举这个包中的消息队列

  1. :阻塞队列 BlockingQueue

  2. 数组阻塞队列 ArrayBlockingQueue

  3. 延迟队列 DelayQueue

  4. 链阻塞队列 LinkedBlockingQueue

  5. 具有优先级的阻塞队列 PriorityBlockingQueue

  6. 同步队列 SynchronousQueue

  7. 阻塞双端队列 BlockingDeque

  8. 链阻塞双端队列 LinkedBlockingDeque

    不同的队列不同的特性决定了队列使用的时机,感兴趣的话你可以详细了解。具体的使用方式我就不赘述了

‘肆’ 怎样用java代码实现一个队列

class Stack<T> {
private Vector<T> v;
public Stack(){
v = new Vector<T>();
}

public T pop(){
if (v.size()==0) return null;
return v.get(v.size()-1);
}

public void push(T t){
v.add(t);
}

public boolean isEmpty(){
return v.size()==0;
}
}

class Queue<T>{
private Vector<T> v;
public Queue(){
v = new Vector<T>();
}
//入队列
public void enqueue(T t){
v.add(t);
}

//出队列
public T dequeue(){
if (v.size()==0) return null;
return v.get(0);
}

public boolean isEmpty(){
return v.size() == 0;
}
}

‘伍’ java中怎么实现队列

public class Queue<E> {
private Object[] data=null;
private int maxSize; //队列容量
private int front; //队列头,允许删除
private int rear; //队列尾,允许插入

//构造函数
public Queue(){
this(10);
}

public Queue(int initialSize){
if(initialSize >=0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}

//判空
public boolean empty(){
return rear==front?true:false;
}

//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException("队列已满,无法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}

//返回队首元素,但不删除
public E peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return (E) data[front];
}
}

//出队
public E poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
E value = (E) data[front]; //保留队列的front端的元素的值
data[front++] = null; //释放队列的front端的元素
return value;
}
}

//队列长度
public int length(){
return rear-front;
}
}

‘陆’ java用数组实现队列

1.1. 队列的数据结构

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

1.2. Java实现

QueueTest

package ch04;

public class QueueTest {

public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(10);

System.out.println(queue.isEmpty());

for (int i = 0; i < 10; i++) {
queue.insert(i);
}
System.out.println(queue.isFull());

while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}

}

class ArrayQueue {
private int[] arrInt;// 内置数组
private int front;// 头指针
private int rear;// 尾指针

public ArrayQueue(int size) {
this.arrInt = new int[size];
front = 0;
rear = -1;
}

/**
* 判断队列是否为空
*
* @return
*/
public boolean isEmpty() {
return front == arrInt.length;
}

/**
* 判断队列是否已满
*
* @return
*/
public boolean isFull() {
return arrInt.length - 1 == rear;
}

/**
* 向队列的队尾插入一个元素
*/
public void insert(int item) {
if (isFull()) {
throw new RuntimeException("队列已满");
}
arrInt[++rear] = item;
}

/**
* 获得对头元素
*
* @return
*/
public int peekFront() {
return arrInt[front];
}

/**
* 获得队尾元素
*
* @return
*/
public int peekRear() {
return arrInt[rear];
}

/**
* 从队列的对头移除一个元素
*
* @return
*/
public int remove() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
return arrInt[front++];
}
}

运行结果如下:

false

true

0

1

2

3

4

5

6

7

8

9

‘柒’ JAVA如何用队列实现并发

如果是抢资源,在不作弊的情况下
按照先来先得的规则
,那么比较简单的实现就是队列
,不管请求的并发多高,如果用线程来实现为用户服务,也就是说
来一个人请求资源那么就启动一个线程,那CPU执行线程总是有顺序的,比如
当前三个人(路人甲路人乙路人丙)请求A资源
,那服务端就起了三个线程为这三个人服务,假设
这三个人不太幸运在请求的时候没有及时的获得CPU时间片,那么他们三个相当于公平竞争CPU资源,而CPU选择运行线程是不确定顺序的
,又假设
选中了路人丙的线程运行那么将其放入队列就好了,路人乙,路人丙以此类推
,那可能会想为什么不及时的处理呢
,因为后续的操作可能是耗时操作对于线程的占用时间较长那请求资源的人多了服务端就可能挂了

‘捌’ JAVA中,常用的队列实现是哪个

队列的实现单纯的是数据结构的问题,既可以用链表结构实现队列,也可以用数组实现。这和语言不是紧密关系,java可以这样实现,c、c++
也可以。

‘玖’ 在JAVA中怎么实现消息队列

java中的消息队列
消息队列是线程间通讯的手段:

importjava.util.*

publicclassMsgQueue{

privateVectorqueue=null;
publicMsgQueue(){
queue=newVector();
}
publicsynchronizedvoidsend(Objecto)
{
queue.addElement(o);
}
publicsynchronizedObjectrecv()
{
if(queue.size()==0)
returnnull;
Objecto=queue.firstElement();
queue.removeElementAt(0);//orqueue[0]=nullcanalsowork
returno;
}
}

因为java中是lockedbyobject的所以添加synchronized就可以用于线程同步锁定对象
可以作为多线程处理多任务的存放task的队列。他的client包括封装好的task类以及thread类

Java的多线程-线程间的通信2009-08-2521:58
1.线程的几种状态
线程有四种状态,任何一个线程肯定处于这四种状态中的一种:
1)产生(New):线程对象已经产生,但尚未被启动,所以无法执行。如通过new产生了一个线程对象后没对它调用start()函数之前。
2)可执行(Runnable):每个支持多线程的系统都有一个排程器,排程器会从线程池中选择一个线程并启动它。当一个线程处于可执行状态时,表示它可能正处于线程池中等待排排程器启动它;也可能它已正在执行。如执行了一个线程对象的start()方法后,线程就处于可执行状态,但显而易见的是此时线程不一定正在执行中。
3)死亡(Dead):当一个线程正常结束,它便处于死亡状态。如一个线程的run()函数执行完毕后线程就进入死亡状态。
4)停滞(Blocked):当一个线程处于停滞状态时,系统排程器就会忽略它,不对它进行排程。当处于停滞状态的线程重新回到可执行状态时,它有可能重新执行。如通过对一个线程调用wait()函数后,线程就进入停滞状态,只有当两次对该线程调用notify或notifyAll后它才能两次回到可执行状态。
2.classThread下的常用函数函数
2.1suspend()、resume()
1)通过suspend()函数,可使线程进入停滞状态。通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态。
2)当调用suspend()函数后,线程不会释放它的“锁标志”。
例11:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<5;i++){
shareVar++;
if(shareVar==5){
this.suspend();//(1)
}}}
else{
System.out.print(Thread.currentThread().getName());
System.out.println("shareVar="+shareVar);
this.resume();//(2)
}}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();//(5)
//t1.start();//(3)
t2.start();//(4)
}}
运行结果为:
t2shareVar=5
i.当代码(5)的t1所产生的线程运行到代码(1)处时,该线程进入停滞状态。然后排程器从线程池中唤起代码(4)的t2所产生的线程,此时shareVar值不为0,所以执行else中的语句。
ii.也许你会问,那执行代码(2)后为什么不会使t1进入可执行状态呢?正如前面所说,t1和t2是两个不同对象的线程,而代码(1)和(2)都只对当前对象进行操作,所以t1所产生的线程执行代码(1)的结果是对象t1的当前线程进入停滞状态;而t2所产生的线程执行代码(2)的结果是把对象t2中的所有处于停滞状态的线程调回到可执行状态。
iii.那现在把代码(4)注释掉,并去掉代码(3)的注释,是不是就能使t1重新回到可执行状态呢?运行结果是什么也不输出。为什么会这样呢?也许你会认为,当代码(5)所产生的线程执行到代码(1)时,它进入停滞状态;而代码(3)所产生的线程和代码(5)所产生的线程是属于同一个对象的,那么就当代码(3)所产生的线程执行到代码(2)时,就可使代码(5)所产生的线程执行回到可执行状态。但是要清楚,suspend()函数只是让当前线程进入停滞状态,但并不释放当前线程所获得的“锁标志”。所以当代码(5)所产生的线程进入停滞状态时,代码(3)所产生的线程仍不能启动,因为当前对象的“锁标志”仍被代码(5)所产生的线程占有。
#p#2.2sleep()
1)sleep()函数有一个参数,通过参数可使线程在指定的时间内进入停滞状态,当指定的时间过后,线程则自动进入可执行状态。
2)当调用sleep()函数后,线程不会释放它的“锁标志”。
例12:
{
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
for(inti=0;i<3;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
try{
Thread.sleep(100);//(4)
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}}
}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();(1)
t1.start();(2)
//t2.start();(3)
}}
运行结果为:
t1:0
t1:1
t1:2
t1:0
t1:1
t1:2
由结果可证明,虽然在run()中执行了sleep(),但是它不会释放对象的“锁标志”,所以除非代码(1)的线程执行完run()函数并释放对象的“锁标志”,否则代码(2)的线程永远不会执行。
如果把代码(2)注释掉,并去掉代码(3)的注释,结果将变为:
t1:0
t2:0
t1:1
t2:1
t1:2
t2:2
由于t1和t2是两个对象的线程,所以当线程t1通过sleep()进入停滞时,排程器会从线程池中调用其它的可执行线程,从而t2线程被启动。
例13:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
for(inti=0;i<5;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
try{
if(Thread.currentThread().getName().equals("t1"))
Thread.sleep(200);
else
Thread.sleep(100);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}
}}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();
//t1.start();
t2.start();
}}
运行结果为:
t1:0
t2:0
t2:1
t1:1
t2:2
t2:3
t1:2
t2:4
t1:3
t1:4
由于线程t1调用了sleep(200),而线程t2调用了sleep(100),所以线程t2处于停滞状态的时间是线程t1的一半,从从结果反映出来的就是线程t2打印两倍次线程t1才打印一次。
#p#2.3yield()
1)通过yield()函数,可使线程进入可执行状态,排程器从可执行状态的线程中重新进行排程。所以调用了yield()的函数也有可能马上被执行。
2)当调用yield()函数后,线程不会释放它的“锁标志”。
例14:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){super(name);
}
publicsynchronizedvoidrun(){for(inti=0;i<4;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
Thread.yield();
}}
}
publicclassTestThread{publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();
t1.start();//(1)
//t2.start();(2)
}
}
运行结果为:
t1:0
t1:1
t1:2
t1:3
t1:0
t1:1
t1:2
t1:3
从结果可知调用yield()时并不会释放对象的“锁标志”。
如果把代码(1)注释掉,并去掉代码(2)的注释,结果为:
t1:0
t1:1
t2:0
t1:2
t2:1
t1:3
t2:2
t2:3
从结果可知,虽然t1线程调用了yield(),但它马上又被执行了。
2.4sleep()和yield()的区别
1)sleep()使当前线程进入停滞状态,所以执行sleep()的线程在指定的时间内肯定不会执行;yield()只是使当前线程重新回到可执行状态,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行。
2)sleep()可使优先级低的线程得到执行的机会,当然也可以让同优先级和高优先级的线程有执行的机会;yield()只能使同优先级的线程有执行的机会。
例15:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicvoidrun(){
for(inti=0;i<4;i++){
System.out.print(Thread.currentThread().getName());
System.out.println(":"+i);
//Thread.yield();(1)
/*(2)*/
try{
Thread.sleep(3000);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}}}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
运行结果为:
t1:0
t1:1
t2:0
t1:2
t2:1
t1:3
t2:2
t2:3
由结果可见,通过sleep()可使优先级较低的线程有执行的机会。注释掉代码(2),并去掉代码(1)的注释,结果为:
t1:0
t1:1
t1:2
t1:3
t2:0
t2:1
t2:2
t2:3
可见,调用yield(),不同优先级的线程永远不会得到执行机会。
2.5join()
使调用join()的线程执行完毕后才能执行其它线程,在一定意义上,它可以实现同步的功能。
例16:
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicvoidrun(){
for(inti=0;i<4;i++){
System.out.println(""+i);
try{
Thread.sleep(3000);
}
catch(InterruptedExceptione){
System.out.println("Interrupted");
}
}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
t1.start();
try{
t1.join();
}
catch(InterruptedExceptione){}
t1.start();
}
}
运行结果为:
0
1
2
3
0
1
2
3
#p#3.classObject下常用的线程函数
wait()、notify()和notifyAll()这三个函数由java.lang.Object类提供,用于协调多个线程对共享数据的存取。
3.1wait()、notify()和notifyAll()
1)wait()函数有两种形式:第一种形式接受一个毫秒值,用于在指定时间长度内暂停线程,使线程进入停滞状态。第二种形式为不带参数,代表waite()在notify()或notifyAll()之前会持续停滞。
2)当对一个对象执行notify()时,会从线程等待池中移走该任意一个线程,并把它放到锁标志等待池中;当对一个对象执行notifyAll()时,会从线程等待池中移走所有该对象的所有线程,并把它们放到锁标志等待池中。
3)当调用wait()后,线程会释放掉它所占有的“锁标志”,从而使线程所在对象中的其它synchronized数据可被别的线程使用。
例17:
下面,我们将对例11中的例子进行修改
{
publicstaticintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<10;i++){
shareVar++;
if(shareVar==5){
try{
this.wait();//(4)
}
catch(InterruptedExceptione){}
}
}
}
if(shareVar!=0){
System.out.print(Thread.currentThread().getName());
System.out.println("shareVar="+shareVar);
this.notify();//(5)
}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
TestThreadMethodt2=newTestThreadMethod("t2");
t1.start();//(1)
//t1.start();(2)
t2.start();//(3)
}}
运行结果为:
t2shareVar=5
因为t1和t2是两个不同对象,所以线程t2调用代码(5)不能唤起线程t1。如果去掉代码(2)的注释,并注释掉代码(3),结果为:
t1shareVar=5
t1shareVar=10
这是因为,当代码(1)的线程执行到代码(4)时,它进入停滞状态,并释放对象的锁状态。接着,代码(2)的线程执行run(),由于此时shareVar值为5,所以执行打印语句并调用代码(5)使代码(1)的线程进入可执行状态,然后代码(2)的线程结束。当代码(1)的线程重新执行后,它接着执行for()循环一直到shareVar=10,然后打印shareVar。
#p#3.2wait()、notify()和synchronized
waite()和notify()因为会对对象的“锁标志”进行操作,所以它们必须在synchronized函数或synchronizedblock中进行调用。如果在non-synchronized函数或non-synchronizedblock中进行调用,虽然能编译通过,但在运行时会发生IllegalMonitorStateException的异常。
例18:
{
publicintshareVar=0;
publicTestThreadMethod(Stringname){
super(name);
newNotifier(this);
}
publicsynchronizedvoidrun(){
if(shareVar==0){
for(inti=0;i<5;i++){
shareVar++;
System.out.println("i="+shareVar);
try{
System.out.println("wait......");
this.wait();
}
catch(InterruptedExceptione){}
}}
}
}
classNotifierextendsThread{
privateTestThreadMethodttm;
Notifier(TestThreadMethodt){
ttm=t;
start();
}
publicvoidrun(){
while(true){
try{
sleep(2000);
}
catch(InterruptedExceptione){}
/*1要同步的不是当前对象的做法*/
synchronized(ttm){
System.out.println("notify......");
ttm.notify();
}}
}
}
publicclassTestThread{
publicstaticvoidmain(String[]args){
TestThreadMethodt1=newTestThreadMethod("t1");
t1.start();
}
}
运行结果为:
i=1
wait......
notify......
i=2
wait......
notify......
i=3
wait......
notify......
i=4
wait......
notify......
i=5
wait......
notify......
4.wait()、notify()、notifyAll()和suspend()、resume()、sleep()的讨论
4.1这两组函数的区别
1)wait()使当前线程进入停滞状态时,还会释放当前线程所占有的“锁标志”,从而使线程对象中的synchronized资源可被对象中别的线程使用;而suspend()和sleep()使当前线程进入停滞状态时不会释放当前线程所占有的“锁标志”。
2)前一组函数必须在synchronized函数或synchronizedblock中调用,否则在运行时会产生错误;而后一组函数可以non-synchronized函数和synchronizedblock中调用。
4.2这两组函数的取舍
Java2已不建议使用后一组函数。因为在调用suspend()时不会释放当前线程所取得的“锁标志”,这样很容易造成“死锁”。

‘拾’ java中如何实现按队列执行任务

package com.tone.example;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.tone.task.TaskProperty;
import com.tone.task.TaskSignature;
import com.tone.task.impl.BasicTask;
import com.tone.task.runner.TaskRunner;

/**
* 任务队列示例程序
* @author zlf
*/
public class TaskExample {
private TaskRunner taskRunner;

/**
* 做任务队列的初始化工作
*/
@Before
public void init() {
// 获取任务运行器
taskRunner = TaskRunner.getInstance();
// 将任务运行器放入线程进行调度
Thread thread = new Thread(taskRunner);
thread.start();
}

/**
* 等待任务执行完成,并做最后的退出工作
*/
@After
public void exit() throws InterruptedException {
Thread.sleep(600);
System.exit(0);
}

/**
* 最简单的任务运行示例
*/
@Test
public void example1() {
// 添加任务到任务运行器
taskRunner.addTask(new BasicTask() {
@Override
public void run() {
System.out.println("This is running in task runner thread, and thread is " + Thread.currentThread());
}
});
}

/**
* 加入优先执行顺序的任务运行器
*/
@Test
public void example2() {
// 添加任务到任务运行器
taskRunner.addTask(new BasicTask(0) {
@Override
public void run() {
System.out.println("This is a normal task");
}
});
taskRunner.addTask(new BasicTask(-1) {
@Override
public void run() {
System.out.println("This is a task a bit high than normal");
}
});
}

/**
* 重复添加的任务只会运行第一个
*/
@Test
public void example3() {
// 添加任务到任务运行器
taskRunner.addTask(new BasicTask(TaskSignature.ONE) {
@Override
public void run() {
System.out.println("This is task one");
}
}, TaskProperty.NOT_REPEAT);
taskRunner.addTask(new BasicTask(TaskSignature.ONE) {
@Override
public void run() {
System.out.println("This is also task one");
}
}, TaskProperty.NOT_REPEAT);
}

/**
* 重复添加的任务只会运行最后一个
*/
@Test
public void example4() {
// 添加任务到任务运行器
taskRunner.addTask(new BasicTask(TaskSignature.ONE) {
@Override
public void run() {
System.out.println("This is task one");
}
}, TaskProperty.NOT_REPEAT_OVERRIDE);
taskRunner.addTask(new BasicTask(TaskSignature.ONE) {
@Override
public void run() {
System.out.println("This is also task one");
}
}, TaskProperty.NOT_REPEAT_OVERRIDE);
}
}

热点内容
ssrpc端怎么删除服务器 发布:2024-05-16 13:53:14 浏览:235
如何配置gdc服务器 发布:2024-05-16 13:53:12 浏览:984
安卓系统阿尔法编译器 发布:2024-05-16 13:51:49 浏览:989
建行支付密码器怎么用 发布:2024-05-16 13:50:19 浏览:985
智能手环如何配置 发布:2024-05-16 13:48:00 浏览:484
shell脚本vim 发布:2024-05-16 13:43:15 浏览:302
征途修改脚本 发布:2024-05-16 13:42:31 浏览:409
xp共享打印机拒绝访问 发布:2024-05-16 13:41:31 浏览:814
常州php招聘 发布:2024-05-16 13:14:52 浏览:590
十二万左右捷达车配置质量怎么样 发布:2024-05-16 12:17:00 浏览:598