|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object nachos.threads.KThread
public class KThread
A KThread is a thread that can be used to execute Nachos kernel code. Nachos allows multiple threads to run concurrently. To create a new thread of execution, first declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating KThread, and forked. For example, a thread that computes pi could be written as follows:
class PiRun implements Runnable { public void run() { // compute pi ... } }
The following code would then create a thread and start it running:
PiRun p = new PiRun(); new KThread(p).fork();
Field Summary | |
---|---|
Object |
schedulingState
Additional state used by schedulers. |
Constructor Summary | |
---|---|
KThread()
Allocate a new KThread. |
|
KThread(Runnable target)
Allocate a new KThread. |
Method Summary | |
---|---|
int |
compareTo(Object o)
Deterministically and consistently compare this thread to another thread. |
static KThread |
currentThread()
Get the current thread. |
static void |
finish()
Finish the current thread and schedule it to be destroyed when it is safe to do so. |
void |
fork()
Causes this thread to begin execution. |
String |
getName()
Get the name of this thread. |
void |
join()
Waits for this thread to finish. |
void |
ready()
Moves this thread to the ready state and adds this to the scheduler's ready queue. |
protected void |
restoreState()
Prepare this thread to be run. |
protected void |
saveState()
Prepare this thread to give up the processor. |
static void |
selfTest()
Tests whether this module is working. |
KThread |
setName(String name)
Set the name of this thread. |
KThread |
setTarget(Runnable target)
Set the target of this thread. |
static void |
sleep()
Relinquish the CPU, because the current thread has either finished or it is blocked. |
String |
toString()
Get the full name of this thread. |
static void |
yield()
Relinquish the CPU if any other thread is ready to run. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public Object schedulingState
PriorityScheduler.ThreadState
Constructor Detail |
---|
public KThread()
public KThread(Runnable target)
target
- the object whose run method is called.Method Detail |
---|
public static KThread currentThread()
public KThread setTarget(Runnable target)
target
- the object whose run method is called.
public KThread setName(String name)
name
- the name to give to this thread.
public String getName()
public String toString()
toString
in class Object
public int compareTo(Object o)
public void fork()
public static void finish()
public static void yield()
Returns immediately if no other thread is ready to run. Otherwise returns when the current thread is chosen to run again by readyQueue.nextThread().
Interrupts are disabled, so that the current thread can atomically add itself to the ready queue and switch to the next thread. On return, restores interrupts to the previous state, in case yield() was called with interrupts disabled.
public static void sleep()
If the current thread is blocked (on a synchronization primitive, i.e. a Semaphore, Lock, or Condition), eventually some thread will wake this thread up, putting it back on the ready queue so that it can be rescheduled. Otherwise, finish() should have scheduled this thread to be destroyed by the next thread to run.
public void ready()
public void join()
protected void restoreState()
protected void saveState()
public static void selfTest()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |