S

 

Thread

One of the important features of the Java language is support for multithreaded (also called concurrent) programming.

A multithreaded program can take advantage of the additional CPUs in a shared-memory multiprocessor architecture in order to execute more quickly. The use of multiple threads can also simplify the design of a program. As an example, consider a server program in which each incoming client request is handled by a dedicated thread.

There are two ways to start Java threads. One way is to subclass the Thread class:
class A extends Thread {
public void run() {
... // code for the new thread to execute
}
}
...
A a = new A(); // create the thread object
a.start(); // start the new thread executing
...


The second way is to implement the Runnable interface:
class B extends ... implements Runnable {
public void run() {
... // code for the new thread to execute
}
}
...
B b = new B(); // create the Runnable object
Thread t = new Thread(b); // create a thread object
t.start(); // start the new thread
...

Stages of a Thread
New before the thread's start() method is called.
Runnable if the thread is in the ready queue.
Running if the thread is executing on the CPU.
Dead after the thread's run() method completes or stop() method is called.
Blocked if the thread is blocked on I/O, a join() method call, or a sleep(ms)method call.
Suspended if the thread's suspend() method is called from the running or runnable states.
Suspended-blocked if the thread's suspend() method is called from the blocked state.

Thread have priorities
Thread priorities (class variables) are:
MAX_PRIORITY
NORM_PRIORITY
MIN_PRIORITY

Priority set and get instance methods include:
setPriority(Thread.priority)
getPriority()

The JVM scheduler usually ensures that the highest priority thread is running on the CPU, pre-empting the currently running thread when necessary, but this is not a guarantee.

The Thread class methods
The following methods are static in class Thread and apply to the calling thread:
Thread.sleep(ms) blocks the calling thread for the specified time.
In Thread.yield(), the calling thread gives up the CPU (but is not guaranteed by the JLS).
Any method can use Thread.currentThread() to get a reference to the thread that called the method, for example, Thread.currentThread().getPriority();.
Use Thread.interrupted() to see if the thread's interrupt() method has been
called (it clears the interrupted flag).


The Thread Instance methods
And here are the instance methods (for example, t.start() in which t is a reference variable to a Thread object):
start(): Start a new thread executing the run() method.
stop(): Terminate the thread (deprecated, do not use).
suspend(): Suspend the thread (deprecated, do not use).
resume(): Resume the suspended thread (deprecated, do not use).
join(): Join with another thread when the latter terminates.
interrupt(): Tell the thread to check for a change in what it should be doing.
isInterrupted(): Check if the thread's interrupt() method has been called (this does not clear the interrupted flag).
isAlive(): Check if the thread has terminated.
setDaemon(boolean): Make the thread a daemon (the JVM ignores this thread when determining if all threads in a program have terminated).
isDaemon(): Check if the thread is a daemon.
setPriority(int): Change the priority of the thread.
getPriority(): Return the prioritiy of the thread.
setName(string): Change the name of the thread to be equal to the argument name.
getName(): Return the name of the thread.

T
Taglibs
thread
throw
throws
transaction
transaction attribute
transaction isolation level
transient
try
type of servers
Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions