Thursday 14 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

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(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()













No comments:

Post a Comment