vmthreadqueue.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 202 行
JAVA
202 行
/*
* $Id: VmThreadQueue.java,v 1.1 2003/11/25 11:41:17 epr Exp $
*/
package org.jnode.vm;
/**
* Queue of VmThread's.
*
* This class is not synchronized, but protected by PragmaUninterruptible's, since it is used by
* the scheduler itself.
*
* @author Ewout Prangsma (epr@users.sourceforge.net)
*/
final class VmThreadQueue extends VmSystemObject {
protected final String name;
private VmThreadQueueEntry first;
private final boolean sortByWakeupTime;
/**
* Create a new instance
*
* @param name
* @param sortByWakeupTime
*/
public VmThreadQueue(String name, boolean sortByWakeupTime) {
this.name = name;
this.sortByWakeupTime = sortByWakeupTime;
}
/**
* Add the given thread to this queue.
*
* @param thread
* @param ignorePriority
* If true, the thread is always added to the back of the list, regarding its
* priority.
* @param caller
* @throws PragmaUninterruptible
*/
final void add(VmThread thread, boolean ignorePriority, String caller) throws PragmaUninterruptible {
if (sortByWakeupTime) {
first = addToQueueSortByWakeupTime(first, thread.sleepQueueEntry, caller);
} else {
first = addToQueue(first, thread.queueEntry, ignorePriority, caller);
}
}
/**
* Gets the first thread in the queue.
*
* @return VmThread
* @throws PragmaUninterruptible
*/
final VmThread first() throws PragmaUninterruptible {
if (first != null) {
return first.thread;
} else {
return null;
}
}
/**
* Remove the given thread from this queue.
*
* @param thread
* @throws PragmaUninterruptible
*/
final void remove(VmThread thread) throws PragmaUninterruptible {
if (sortByWakeupTime) {
first = removeFromQueue(first, thread.sleepQueueEntry);
} else {
first = removeFromQueue(first, thread.queueEntry);
}
}
/**
* Is this queue empty?
*
* @return boolean
* @throws PragmaUninterruptible
*/
final boolean isEmpty() throws PragmaUninterruptible {
return (first == null);
}
/**
* Add the given thread to the given queue. The thread is added after all thread with equal or
* higher priority.
*
* @param queue
* @param entry
* @param ignorePriority
* If true, the thread is always added to the back of the list, regarding its
* priority.
* @param caller
* @return The new queue.
* @throws PragmaUninterruptible
*/
private final VmThreadQueueEntry addToQueue(VmThreadQueueEntry queue, VmThreadQueueEntry entry, boolean ignorePriority, String caller) throws PragmaUninterruptible {
entry.setInUse(this, caller);
if (queue == null) {
return entry;
} else if (ignorePriority) {
VmThreadQueueEntry t = queue;
while (t.next != null) {
t = t.next;
}
t.next = entry;
return queue;
} else {
final int priority = entry.getThread().priority;
VmThreadQueueEntry t = queue;
if (priority > t.thread.priority) {
entry.setNext(t);
return entry;
}
while (t.next != null) {
if (priority > t.next.thread.priority) {
entry.next = t.next;
t.next = entry;
return queue;
}
t = t.next;
}
t.next = entry;
return queue;
}
}
/**
* Add the given thread to the given queue such that the queue is sorted by wakeup time. The
* nearest wakeup time is the first element in the queue.
*
* @param queue
* @param entry
* @param caller
* @return The new queue.
* @throws PragmaUninterruptible
*/
private final VmThreadQueueEntry addToQueueSortByWakeupTime(VmThreadQueueEntry queue, VmThreadQueueEntry entry, String caller) throws PragmaUninterruptible {
entry.setInUse(this, caller);
if (queue == null) {
return entry;
} else {
long threadWakeup = entry.thread.wakeupTime;
VmThreadQueueEntry t = queue;
if (threadWakeup < t.thread.wakeupTime) {
entry.next = t;
return entry;
}
while (t.next != null) {
if (threadWakeup < t.next.thread.wakeupTime) {
entry.next = t.next;
t.next = entry;
return queue;
}
t = t.next;
}
t.next = entry;
return queue;
}
}
/**
* Remove the given thread from the given queue.
*
* @param queue
* @param entry
* @return The new queue.
* @throws PragmaUninterruptible
*/
private static VmThreadQueueEntry removeFromQueue(VmThreadQueueEntry queue, VmThreadQueueEntry entry) throws PragmaUninterruptible {
if (queue == null) {
return queue;
} else if (entry == null) {
return queue;
} else if (queue == entry) {
VmThreadQueueEntry result = entry.next;
entry.next = null;
entry.setInUse(null, null);
return result;
} else {
VmThreadQueueEntry t = queue;
while ((t.next != null) && (t.next != entry)) {
t = t.next;
}
if (t.next == entry) {
t.next = entry.next;
entry.next = null;
entry.setInUse(null, null);
}
return queue;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?