/* related posts with thumb nails */

Thread Class Methods:

Thread is a class found in java.lang package. It provides several different methods to perform thread tasks and control thread behaviors. The methods of Thread class with their meanings are listed below:

Constructors:  Thread class has many constructors out of which following are two:
Syntax:   public Thread(String threadName)
public Thread()

Methods:
1.      start() method:  This method is used to start a new thread. When this method is called the thread enters the runnable state and this automatically invokes the run( ) method .
Syntax:  void start( )

2.      run() method: This method is the most important method in the thread like its heart and soul. It contains the statements that define the actual task of the Thread. It should be overridden in our class in the class of extending Thread class or implement in the case of implementing Runnable interface.
Syntax:   void run( )
{
     // thread Statements  
}
3.      sleep() method: This method is used to block the currently executing thread for the specific time. After the elapse of the time the thread automatically comes into runnable state.
Syntax:  void sleep(long time-in-milliseconds)
4.      stop() method: This method is used to stop the running thread even before the completion of the task.
Syntax: void stop()
5.      wait() method: This method is used to block the currently executing thread until the thread invokes notify() or notifyall() methods.
Syntax: void wait()
6.      suspend() method: This method is used to block the currently executing thread until the thread invokes resume() method.
Syntax: void suspend()
7.      resume() method: This method is used to bring the thread from blocked state to runnable state when it is blocked by suspend() method.
Syntax: void resume()
8.      yield() method: This method is used to bring the blocked thread to runnable state. If we want a thread to give a chance to run before its turn comes, we can use the yield() method.
Syntax: void yield()
9.      setPriority( ) method: This method is used to set the priority of the thread. The priority is an integer value that ranges from 1 to 10. The default setting is NORM_PRIORITY whose value is 5.  The other constants are:  MIN_PRIORITY (= 1) and MAX_ PRIORITY (= 10).
Syntax:  setPriority(int priority)
10.  getPriority( ) method: This method is used to get the priority of the thread. It returns integer value.
Syntax:  int getPriority( )
Syntax: void wait()
11.  setName( ) method: This method is used to set a name to a thread.
Syntax:  void setName(String)
12.  getName( ) method: This method is used to get the name of the thread. It returns String value.
Syntax:  String getName( )
13.  join() method: This method is used to lock a thread. No other thread can interrupt the locked thread until its completion.
14.  isAlive() method: This method returns true if the thread is alive. The thread is said to be alive when it is in  runnable or running or blocked state i.e. it has been started and has not yet died.
Syntax:  boolean isAlive( )
Related Topics:

0 comments:

Post a Comment