Java 在NACHOS操作系统中实现KThread类
我正在尝试在nachos OS中实现KThread.join()方法。我的示例代码片段如下所示:Java 在NACHOS操作系统中实现KThread类,java,multithreading,operating-system,nachos,Java,Multithreading,Operating System,Nachos,我正在尝试在nachos OS中实现KThread.join()方法。我的示例代码片段如下所示: private KThread toJoin = null; public void join() { Lib.debug(dbgThread, "Joining to thread: " + toString()); Lib.assertTrue(this != currentThread); Lib.assertTrue(toJoin == null); boolean i
private KThread toJoin = null;
public void join() {
Lib.debug(dbgThread, "Joining to thread: " + toString());
Lib.assertTrue(this != currentThread);
Lib.assertTrue(toJoin == null);
boolean intStatus = Machine.interrupt().disable();
if (status != statusFinished)
{
toJoin = KThread.currentThread();
KThread.sleep();
}
Machine.interrupt().restore(intStatus);
}
public static void finish() {
Lib.debug(dbgThread, "Finishing thread: " + currentThread.toString());
Machine.interrupt().disable();
Machine.autoGrader().finishingCurrentThread();
Lib.assertTrue(toBeDestroyed == null); // what is being done in this line?
toBeDestroyed = currentThread;
if (currentThread.toJoin != null)
{
currentThread.toJoin.ready(); ////what is being done in this line?
}
currentThread.status = statusFinished;
sleep();
}
我的问题是,如果我创建一个父线程,并在其中创建一个子线程并调用child.join()方法,那么父线程会发生什么情况?根据我的说法,它将进入睡眠状态,直到子线程完成它的任务。我说得对吗?关于这一点,我还有一些问题:
我会先说的,关于这门课我不太记得了。我记得它工作的方式是孩子会调用join,然后家长必须在孩子恢复之前完成 回答您的问题:
public void join() {
Lib.debug(dbgThread, "Joining to thread: " + toString());
Lib.assertTrue(this != currentThread);
// Entering a critical Section
Machine.interrupt().disable();
// Check if the queue has been instantiated
if(joinerQueue == null){
// Instantiate it
joinerQueue = ThreadedKernel.scheduler.newThreadQueue(true);
}
// Check if the thread is finished
if (status == statusFinished) {
return;
}
else {
// We give joinerQueue the lock of this in order to transfer the priority of this thread
joinerQueue.acquire(this);
// we are adding the currentThread to the joinerQueue
joinerQueue.waitForAccess(currentThread);
// We sleep the currentThread because we need to wait until this thread finishes
// When this thread finishes we will wake up in the finish() method
currentThread.sleep();
}
Machine.interrupt().enable();
}
public static void finish() {
Lib.debug(dbgThread, "Finishing thread: " + currentThread.toString());
// Critical Section
Machine.interrupt().disable();
// Get the threads which are waiting for the currentThread to finish
ThreadQueue currentQueue = currentThread.joinerQueue;
// if currentQueue is not null, means there are threads waiting to join
if(currentQueue != null) {
// Get the first thread from the queue
KThread curThread = currentQueue.nextThread();
// While there are more threads, ready them up
while ( curThread != null ) {
curThread.ready();
currentThread.joinerQueue.acquire(curThread);
// Get the next thread to ready up
curThread = currentQueue.nextThread();
}
}
Machine.autoGrader().finishingCurrentThread();
Lib.assertTrue(toBeDestroyed == null);
toBeDestroyed = currentThread;
currentThread.status = statusFinished;
sleep();
}