Thursday, 8 March 2012

Creating threads in Java



The “main” thread: 

•When most of the Java programs are executed, a single thread is executed. That thread is the “main” thread present in the program.
•“main” is important for two reasons:
1) It acts as the parent thread from which we can create multiple child threads.
2) In most of the cases, “main” thread is the last thread that is to complete execution, which is used to perform some shutdown operations.
•The “main” thread is created automatically.
•The “main” thread can be controlled using a “Thread” object.


•We can obtain a reference to the current thread by using the “currentThread()” method of the “Thread” class.
•The syntax of “currentThread()” method is as follows:
public static Thread currentThread()
•We can set and get the name of a thread by using the following methods available in “Thread” class:
1) final void setName(String threadname)
2) final String getName()
•We can suspend a thread by using the sleep() method available in the “Thread” class.

•Syntax of the “sleep()” method is as follows:
static void sleep(long milliseconds) throws InterruptedException
static void sleep(long milliseconds, int nanoseconds) throws InterruptedException


Creating a Thread:

•In Java threads can be created in two ways:
1) Implementing the “Runnable” interface.
2) Extending the “Thread” class.
•Implementing “Runnable” interface:
•In order to create a thread on the object of a certain class, that class must implement the “Runnable” interface.
•Runnable abstracts a single unit of executable code.
•When a class implements the “Runnable” interface, the class must override the “run()” method available in the “Runnable” interface.
•“run()” method is the entry point of every thread.

•“run()” consists of the executable code for a thread.
•Syntax of run() method is:
public void run()
•The class which implements the “Runnable” interface must instantiate the “Thread” class from within the class.
•After the thread is created, it must be started by using the “start()” method which is available in the “Thread” class.
•The “start()” method calls the “run()” method, which starts the execution of the thread.
•Syntax of start() method is:
void start()



•Extending the “Thread” class:
•Another way of creating the thread is to extend the “Thread” class.
•After extending the “Thread” class, the class must be instantiated.
•After creating the thread the start() method must be invoked which in turn invokes the run() method.


•In the two ways available to create the threads, which one to use?
•In your program, if you want to implement the run() method along with the other methods available in the “Thread” class, then you will be extending the “Thread” class for creating the threads.
•In your program, if you want to only implement the run() method, then you will be implementing the “Runnable” interface for creating the threads.


Multiple Threads:

•We can create multiple threads by creating multiple instances of the class which either implement the "Runnable” interface or extend the “Thread” class. 

No comments:

Post a Comment