并发编程:并发集合:按优先级排序的阻塞线程安全队列PriorityBlockingQueue

目录

PriorityBlockingQueue

一、主程序

二、元素类

三、任务类

四、执行结果


PriorityBlockingQueue

所有插入PriorityBlockingQueue的元素都必须实现Comparable接口,该接口需要重写compareTo方法,它比较当前对象与参数对象的优先级,如果对象的优先级更高,则返回-1,参数的优先级更高,则返回1,相同返回0。

或者PriorityBlockingQueue在构造时,传入一个Comparator对象。

一、主程序

package xyz.jangle.thread.test.n7_4.priorityblockingqueue;

import java.util.concurrent.PriorityBlockingQueue;

/**
 * 7.4、优先级排序的阻塞线程安全队列
 * 所有插入PriorityBlockingQueue的元素都必须实现Comparable接口,该接口需要重写compareTo方法,
 * 它比较当前对象与参数对象的优先级,如果对象的优先级更高,则返回-1,参数的优先级更高,则返回1,相同返回0。
 * 
 * @author jangle
 * @email [email protected]
 * @time 2020年9月12日 上午9:56:45
 * 
 */
public class M {

	public static void main(String[] args) {
		PriorityBlockingQueue queue = new PriorityBlockingQueue<>();
		Thread[] threads = new Thread[5];
		for (int i = 0; i < threads.length; i++) {
			var task = new Task(i, queue);
			threads[i] = new Thread(task);
		}
		for (int i = 0; i < threads.length; i++) {
			threads[i].start();
		}
		// 等待5个线程执行完成
		for (int i = 0; i < threads.length; i++) {
			try {
				threads[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("M: QueueSize:" + queue.size());
		for (int i = 0; i < threads.length * 1000; i++) {
			var event = queue.poll();
			System.out.println("Thread " + event.getThread() + ": Priority " + event.getPriority());
		}
		System.out.println("M: Queue Size : " + queue.size());
		System.out.println("M: End ... ");

	}

}

二、元素类

package xyz.jangle.thread.test.n7_4.priorityblockingqueue;

import java.util.concurrent.PriorityBlockingQueue;

/**
 * 	用于插入队列的元素类
 * @author jangle
 * @email [email protected]
 * @time 2020年9月12日 上午9:57:50
 * 
 */
public class Event implements Comparable {

	private final int thread;

	private final int priority;

	public Event(int thread, int priority) {
		super();
		this.thread = thread;
		this.priority = priority;
	}

	public int getThread() {
		return thread;
	}

	public int getPriority() {
		return priority;
	}

	@Override
	public int compareTo(Event o) {
		if (this.priority > o.getPriority())
			return -1;	// 对象比参数优先,返回-1 , 否则返回1
		if (this.priority < o.getPriority())
			return 1;
		return 0;
	}
	
	public static void main(String[] args) {
		PriorityBlockingQueue queue = new PriorityBlockingQueue();
		Event event = new Event(1,2);
		Event event2 = new Event(3,4);
		queue.add(event);
		queue.add(event2);
		System.out.println(queue.poll().getPriority());
	}

}

三、任务类

package xyz.jangle.thread.test.n7_4.priorityblockingqueue;

import java.util.concurrent.PriorityBlockingQueue;

/**
 * 	任务类(为队列添加元素)
 * @author jangle
 * @email [email protected]
 * @time 2020年9月12日 上午10:01:01
 * 
 */
public class Task implements Runnable {

	// 身份ID
	private final int id;

	private final PriorityBlockingQueue queue;

	public Task(int id, PriorityBlockingQueue queue) {
		super();
		this.id = id;
		this.queue = queue;
	}

	@Override
	public void run() {
		for (int i = 0; i < 1000; i++) {
			Event event = new Event(id, i);
			queue.add(event);
		}
	}

}

四、执行结果

...
...
Thread 0: Priority 6
Thread 4: Priority 6
Thread 2: Priority 6
Thread 3: Priority 6
Thread 1: Priority 6
Thread 2: Priority 5
Thread 3: Priority 5
Thread 0: Priority 5
Thread 1: Priority 5
Thread 4: Priority 5
Thread 1: Priority 4
Thread 4: Priority 4
Thread 3: Priority 4
Thread 0: Priority 4
Thread 2: Priority 4
Thread 0: Priority 3
Thread 2: Priority 3
Thread 1: Priority 3
Thread 4: Priority 3
Thread 3: Priority 3
Thread 3: Priority 2
Thread 2: Priority 2
Thread 0: Priority 2
Thread 4: Priority 2
Thread 1: Priority 2
Thread 3: Priority 1
Thread 0: Priority 1
Thread 1: Priority 1
Thread 4: Priority 1
Thread 2: Priority 1
Thread 4: Priority 0
Thread 2: Priority 0
Thread 3: Priority 0
Thread 0: Priority 0
Thread 1: Priority 0
M: Queue Size : 0
M: End ... 

 

你可能感兴趣的:(并发编程,JavaBase,并发集合,队列,多线程,java,queue)