threadB.start();
Runnable r = new Runnable() {
public void run() {
Thread threadC =
new Thread(makeRunnable(), "threadC");
threadC.start();
}
};
Thread threadD = new Thread(r, "threadD");
threadD.setPriority(7);
threadD.start();
try {
Thread.sleep(3000);
}
catch (InterruptedException x) {}
threadA.setPriority(3);
System.out.println("in main() - threadA.getPriority()=" +
threadA.getPriority());
}
}
先进先出算法(FIFO)
采用数组实现:
SimpleObjectFIFO.java
public class SimpleObjectFIFO
extends Object {
//声明一数组用来保存数据
private Object[] queue;
//用于保存数组的总长度
private int capacity;
//用于保存当前数组的长度
private int size;
//要添加的位置
private int head;
//要删除的位置
private int tail;
public SimpleObjectFIFO(int cap) {
//初始化数组容器大小
capacity = (cap > 0) ? cap : 1; // at least 1
//开辟一个capacity大小的对象数组
queue = new Object[capacity];
//要添加的位置为0
head = 0;
//要删除的位置为0
tail = 0;
//数组的内容为0
size = 0;
}
public synchronized int getSize() {
//得到数组的当前大小
return size;
}
public synchronized boolean isFull() {
//判断数组是否已经存满
return (size == capacity);
}
public synchronized void add(Object obj) throws InterruptedException {
//向数组中添加一个元素
//如果数组已经满了,则让其他线程等待
while (isFull()) {
wait();