我通常使用一個名爲Simulation的類來前進時間。我沒有使用C++,但是我已經在Java中進行了線程化,這可以加快時間並根據時間表(或計劃時間的隨機事件)激活事件。您可以將其轉化爲C++或用於查看面向對象的實現是如何實現的。
package adventure;
public class Simulation extends Thread {
private PriorityQueue prioQueue;
Simulation() {
prioQueue = new PriorityQueue();
start();
}
public void wakeMeAfter(Wakeable SleepingObject, double time) {
prioQueue.enqueue(SleepingObject, System.currentTimeMillis() + time);
}
public void run() {
while (true) {
try {
sleep(5);
if (prioQueue.getFirstTime() <= System.currentTimeMillis()) {
((Wakeable) prioQueue.getFirst()).wakeup();
prioQueue.dequeue();
}
} catch (InterruptedException e) {
}
}
}
}
要使用它,你只需實例並添加你的對象:
` Simulation sim = new Simulation();
// Load images to be used as appearance-parameter for persons
Image studAppearance = loadPicture("Person.gif");
// --- Add new persons here ---
new WalkingPerson(sim, this, "Peter", studAppearance);
http://www.cplusplus.com/reference/ctime/有什麼問題? – thiagoh 2013-02-25 04:35:20
它會完成這項工作。但是如果你採取這種方法,它會讓你對熟悉互斥體的人產生一些傷心。 – 2013-02-25 04:36:05
爲什麼有這樣的線程比替代方案更好? – 2013-02-25 04:40:08