ewe.sys
Class mThread

java.lang.Object
  extended byewe.sys.mThread
All Implemented Interfaces:
Runnable

public class mThread
extends Object
implements Runnable

The mThread class is used as a substitute for the java.lang.Thread class and is used in Ewe as a convenient way of converting simple Java algorithms that use Threads to work in Ewe. A simple change of "Thread" to "mThread" will take care of most conversions.

An important thing to note is that this is not a true pre-emptive thread. This is simply a Runnable object that is run by a Coroutine. Therefore other mThreads or Coroutines will not be active while an mThread is running. Calling sleep() within an mThread will allow other Coroutines to run.


Field Summary
protected  boolean daemon
           
static int MAX_PRIORITY
           
static int MIN_PRIORITY
           
protected  Runnable myRunnable
           
protected  String name
           
static int NORM_PRIORITY
           
protected  int priority
           
 
Constructor Summary
mThread()
           
mThread(mThreadGroup group, Runnable run)
           
mThread(mThreadGroup group, Runnable run, String name)
           
mThread(mThreadGroup group, String name)
           
mThread(Runnable run)
           
mThread(Runnable run, String name)
           
mThread(String name)
           
 
Method Summary
static mThread currentThread()
          Return the current running mThread.
 String getName()
          Get the name of this thread.
 int getPriority()
          Get the priority level of this thread.
 mThreadGroup getThreadGroup()
           
 void interrupt()
           
static boolean inThread()
          This returns true if a the current thread is a true mThread (i.e.
 boolean isAlive()
          Checks if the thread is alive.
 boolean isDaemon()
          Checks if this thread is a daemon thread.
 void join()
          Wait for this mThread to die.
 void join(long millis)
          Wait for this mThread to die for at most millis milliseconds.
 void join(long millis, int nano)
          Wait for this mThread to die for at most millis milliseconds.
static boolean nap(int time)
          If the current thread is a Coroutine thread, it will cause a sleep for the specified length of time, otherwise this call is ignored.
static int nap(int iterations, int time)
          This will cause the running mThread to sleep for time (in milliseconds) every iterations calls to this method.
 void run()
           
 void setDaemon(boolean daemon)
          Sets whether this thread is to be a daemon thread or not - but as of 1.43 setting an thread to be a daemon thread has no effect on how the thread is treated or how it operates.
 void setName(String name)
          Set the name of this thread.
 void setPriority(int newPriority)
          Set the priority level of this thread.
static void sleep(long millis)
          Cause the current mThread to sleep for the specified length of time, unless interrupted by another thread.
static void sleep(long millis, int nanos)
          Cause the current mThread to sleep for the specified length of time, unless interrupted by another thread.
 void start()
          Start the mThread running at the soonest opportunity.
static void waitForResume()
          This is used with native methods where the native method must run in its own native thread.
static boolean waitForResume(long millis)
          This is used with native methods where the native method must run in its own native thread.
static void yield()
          Give other threads a chance to run.
static void yield(int everyMillis)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

MAX_PRIORITY

public static final int MAX_PRIORITY
See Also:
Constant Field Values

MIN_PRIORITY

public static final int MIN_PRIORITY
See Also:
Constant Field Values

NORM_PRIORITY

public static final int NORM_PRIORITY
See Also:
Constant Field Values

myRunnable

protected Runnable myRunnable

name

protected String name

priority

protected int priority

daemon

protected boolean daemon
Constructor Detail

mThread

public mThread()

mThread

public mThread(mThreadGroup group,
               Runnable run,
               String name)

mThread

public mThread(Runnable run)

mThread

public mThread(Runnable run,
               String name)

mThread

public mThread(String name)

mThread

public mThread(mThreadGroup group,
               String name)

mThread

public mThread(mThreadGroup group,
               Runnable run)
Method Detail

getPriority

public int getPriority()
Get the priority level of this thread.


getThreadGroup

public final mThreadGroup getThreadGroup()

setPriority

public final void setPriority(int newPriority)
Set the priority level of this thread.


setName

public void setName(String name)
Set the name of this thread.


getName

public String getName()
Get the name of this thread.


isAlive

public final boolean isAlive()
Checks if the thread is alive. A thread is alive if it has been started and has not died.

Returns:
true if the thread has been started and has not died.

isDaemon

public final boolean isDaemon()
Checks if this thread is a daemon thread.


setDaemon

public final void setDaemon(boolean daemon)
Sets whether this thread is to be a daemon thread or not - but as of 1.43 setting an thread to be a daemon thread has no effect on how the thread is treated or how it operates. This method is added simply to make porting Java applications easier.

Parameters:
daemon - true to mark this thread as a daemon thread. False otherwise.
Throws:
IllegalThreadStateException - if the thread has been already started.

waitForResume

public static void waitForResume()
                          throws InterruptedException
This is used with native methods where the native method must run in its own native thread. This call will put the current mThread to sleep (allowing others to run) indefinitely until it is interrupted or until the native thread wakes it up to indicate that its task is complete.

This method is the same as waitForResume(-1).

Throws:
InterruptedException - if interrupt() was called on the thread.

waitForResume

public static boolean waitForResume(long millis)
                             throws InterruptedException
This is used with native methods where the native method must run in its own native thread. This call will put the current mThread to sleep (allowing others to run) for a maximum length of time until it is interrupted or until the native thread wakes it up to indicate that its task is complete.

Parameters:
millis - The maximum length of time to wait, or -1 to wait indefinitely.
Returns:
true if the native thread woke up the mThread, false if it waited until the maximum time expired.
Throws:
InterruptedException - if interrupt() was called on the thread.

sleep

public static void sleep(long millis)
                  throws InterruptedException
Cause the current mThread to sleep for the specified length of time, unless interrupted by another thread.

Parameters:
millis - The length of time in milliseconds to sleep. A sleep of -1 will cause
Throws:
InterruptedException - - if interrupted by another thread.

sleep

public static void sleep(long millis,
                         int nanos)
                  throws InterruptedException
Cause the current mThread to sleep for the specified length of time, unless interrupted by another thread.

Parameters:
millis - The length of time in milliseconds to sleep.
nanos - An additional time in nanoseconds to sleep.
Throws:
InterruptedException - - if interrupted by another thread.

run

public void run()
Specified by:
run in interface Runnable

yield

public static void yield()
Give other threads a chance to run.


yield

public static void yield(int everyMillis)

interrupt

public void interrupt()

start

public void start()
Start the mThread running at the soonest opportunity. This causes the run() method to be called.


join

public void join()
          throws InterruptedException
Wait for this mThread to die.

Throws:
InterruptedException - if interrupt() was called on it.

join

public void join(long millis)
          throws InterruptedException
Wait for this mThread to die for at most millis milliseconds.

Parameters:
millis - The maximum time to wait for the mThread to die. A value of 0 means to wait forever.
Throws:
InterruptedException - if interrupt() was called on it.

join

public void join(long millis,
                 int nano)
          throws InterruptedException
Wait for this mThread to die for at most millis milliseconds.

Parameters:
millis - The maximum time to wait for the mThread to die. A value of 0 means to wait forever.
nano - An additional number of nanoseconds to wait.
Throws:
InterruptedException - if interrupt() was called on it.

nap

public static final int nap(int iterations,
                            int time)
This will cause the running mThread to sleep for time (in milliseconds) every iterations calls to this method. Therefore nap(10,100) will cause the current mThread to sleep for 100 milliseconds every 10 times this method is called. This is useful to place within loops that require a lot of time to complete.


nap

public static final boolean nap(int time)
If the current thread is a Coroutine thread, it will cause a sleep for the specified length of time, otherwise this call is ignored.

Parameters:
time - the length of time to nap for - it can be 0, but should not be < 0.
Returns:
true if a sleep occured, false if it did not.

currentThread

public static mThread currentThread()
Return the current running mThread. This will return an mThread even if this thread is not running in a mThread. That is because there is a "simulated" mThread that is used to represent non-mThread threads. If you want to know if the current thread actually is running in an mThread, then call inThread()


inThread

public static boolean inThread()
This returns true if a the current thread is a true mThread (i.e. is being run in a Coroutine).