import com.facebook.internal.WorkQueue; //導入方法依賴的package包/類
@Override
public void run() {
// Each iteration runs a random action against the WorkQueue.
if (iterationIndex++ < iterationCount) {
final int sleepWeight = 80;
final int trackThisWeight = 10;
final int prioritizeTrackedWeight = 6;
final int validateWeight = 2;
int weight = 0;
final int n = random.nextInt(sleepWeight + trackThisWeight + prioritizeTrackedWeight + validateWeight);
WorkQueue.WorkItem workItem = manager.addActiveWorkItem(this);
if (n < (weight += sleepWeight)) {
// Sleep
try {
Thread.sleep(n/4);
} catch (InterruptedException e) {
}
} else if (n < (weight += trackThisWeight)) {
// Track this work item to activate later
synchronized (tracked) {
tracked.add(workItem);
}
} else if (n < (weight += prioritizeTrackedWeight)) {
// Background all pending items, prioritize tracked items, and clear tracked list
ArrayList items = new ArrayList();
synchronized (tracked) {
items.addAll(tracked);
tracked.clear();
}
for (WorkQueue.WorkItem item : items) {
item.moveToFront();
}
} else {
// Validate
manager.validate();
}
} else {
// Also have all threads validate once they are done.
manager.validate();
synchronized (this) {
isDone = true;
this.notifyAll();
}
}
}