Wednesday 13 March 2013

java.lang Thread


java.lang
Thread

Declaration
public class Thread implements Runnable
java.lang.Object
|
+--java.lang.Thread
All Implemented Interfaces: Runnable
Description
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple
threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower
priority.
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread.
This subclass should override the run method of class Thread. An instance of the subclass can then be
allocated and started. For example, a thread that computes primes larger than a stated value could be written as
follows:
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();
The other way to create a thread is to 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 Thread, and started. The same example in this other style looks like the following:
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
The following code would then create a thread and start it running:

MIN_PRIORITY
242
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
Since: JDK1.0, CLDC 1.0
See Also: Runnable, Runtime.exit(int), run()

Fields
static int MAX_PRIORITY
static int MIN_PRIORITY
static int NORM_PRIORITY
Constructors
Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
Thread(String name)
Methods
static int activeCount()
static Thread currentThread()
String getName()
int getPriority()
void interrupt()
boolean isAlive()
void join()
void run()
void setPriority(int newPriority)
static void sleep(long millis)
void start()
String toString()
static void yield()

Methods inherited from class Object
equals(Object), getClass(), hashCode(), notify(), notifyAll(), wait(), wait(), wait()

Fields
MIN_PRIORITY
Declaration:
public static final int MIN_PRIORITY

Description:
The minimum priority that a thread can have.
NORM_PRIORITY
Declaration:
public static final int NORM_PRIORITY
Description:
The default priority that is assigned to a thread.
MAX_PRIORITY
Declaration:
public static final int MAX_PRIORITY
Description:
The maximum priority that a thread can have.
Constructors
Thread()
Declaration:
public Thread()
Description:
Allocates a new Thread object.
Threads created this way must have overridden their run() method to actually do anything.
See Also: Runnable
Thread(String)
Declaration:
public Thread(java.lang.String name)
Description:
Allocates a new Thread object with the given name. Threads created this way must have overridden their
run() method to actually do anything.
Parameters:
name - the name of the new thread.
Thread(Runnable)
Declaration:
public Thread(java.lang.Runnable target)
Description:
Allocates a new Thread object with a specific target object whose run method is called.
Parameters:
target - the object whose run method is called.

Thread java.lang
Thread(Runnable, String)

Thread(Runnable, String)
Declaration:
public Thread(java.lang.Runnable target, java.lang.String name)
Description:
Allocates a new Thread object with the given target and name.
Parameters:
target - the object whose run method is called.
name - the name of the new thread.
Methods
currentThread()
Declaration:
public static java.lang.Thread currentThread()
Description:
Returns a reference to the currently executing Thread object.
Returns: the currently executing thread.
yield()
Declaration:
public static void yield()
Description:
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
sleep(long)
Declaration:
public static void sleep(long millis)
throws InterruptedException
Description:
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of
milliseconds. The thread does not lose ownership of any monitors.
Parameters:
millis - the length of time to sleep in milliseconds.
Throws:
InterruptedException - if another thread has interrupted the current thread. The interrupted
status of the current thread is cleared when this exception is thrown.
See Also: Object.notify()
start()
Declaration:
public void start()

Description:
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the
start method) and the other thread (which executes its run method).
Throws:
IllegalThreadStateException - if the thread was already started.
See Also: run()
run()
Declaration:
public void run()
Description:
If this thread was constructed using a separate Runnable run object, then that Runnable object’s run
method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.
Specified By: run in interface Runnable
See Also: start(), Runnable.run()
interrupt()
Declaration:
public void interrupt()
Description:
Interrupts this thread. In an implementation conforming to the CLDC Specification, this operation is not
required to cancel or clean up any pending I/O operations that the thread may be waiting for.
Since: JDK 1.0, CLDC 1.1
isAlive()
Declaration:
public final boolean isAlive()
Description:
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
Returns: true if this thread is alive; false otherwise.
setPriority(int)
Declaration:
public final void setPriority(int newPriority)
Description:
Changes the priority of this thread.
Parameters:
newPriority - priority to set this thread to
Throws:
IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to
MAX_PRIORITY.

getPriority()

See Also: getPriority(), MAX_PRIORITY, MIN_PRIORITY
getPriority()
Declaration:
public final int getPriority()
Description:
Returns this thread’s priority.
Returns: this thread’s priority.
See Also: setPriority(int)
getName()
Declaration:
public final java.lang.String getName()
Description:
Returns this thread’s name. Note that in CLDC the name of the thread can only be set when creating the
thread.
Returns: this thread’s name.
activeCount()
Declaration:
public static int activeCount()
Description:
Returns the current number of active threads in the virtual machine.
Returns: the current number of active threads.
join()
Declaration:
public final void join()
throws InterruptedException
Description:
Waits for this thread to die.
Throws:
InterruptedException - if another thread has interrupted the current thread. The interrupted
status of the current thread is cleared when this exception is thrown.
toString()
Declaration:
public java.lang.String toString()
Description:
Returns a string representation of this thread, including the thread’s name and priority.
Overrides: toString in class Object
Returns: a string representation of this thread.









No comments:

Post a Comment