Friday, 28 February 2014

Creating first thread in java

Thread Creation:
Threads can be created either by,

  • Extending the java.lang.Thread Class or by

  • Implementing the Runnable interface



Example: Extending Thread class
package thread;
/**
* @author Sivaranjani D
*
*/
public class ExtendingThread {
public static void main(String args[]){
ThreadA thread = new ThreadA();
thread.setName("ThreadA");
//thread.run(); //turn on this and turn off the thread.start() and see the difference
thread.start();
System.out.println(Thread.currentThread().getName());
}
}

class ThreadA extends Thread{
public void run(){
for (int i=0; i<5;i++){
System.out.println("Executed by "+Thread.currentThread().getName()+" and output: "+i);
}
}

}

Output
main
Executed by ThreadA and output: 0
Executed by ThreadA and output: 1
Executed by ThreadA and output: 2
Executed by ThreadA and output: 3
Executed by ThreadA and output: 4

Explanation:

  1. Class ThreadA extends the java.lang.Thread class and it will override the run method

  2. The code inside the run method is the job which will be executed by thread when calling the start method on it

  3. Thread is instantiated in the main method by calling "ThreadA thread = new ThreadA();" . At this stage thread is not alive and it will be in new state.

  4. By default thread has name starts like "thread-1". This can be changed by calling the "setName(String name)" method on the thread instance.

  5. Calling start method on thread instance creates the new stack and thread is set to be alive at this stage. This state is called as runnable.

  6. The limitation of creating threads by extending Thread class is, here the job can be executed by only one thread.


Example:Implementing the Runnable Interface
package thread;
/**
* @author Sivaranjani D
*
*/
public class ImplementingRunnable {
public static void main(String args[]){
ThreadB job = new ThreadB();
Thread servant1 = new Thread(job,"Servant1");
servant1.start();

Thread servant2 = new Thread(job,"Srevant2");
servant2.start();
}
}

class ThreadB implements Runnable{
public void run(){
for(int i=0;i<5;i++){

System.out.println("Executed by "+Thread.currentThread().getName()+" and output: "+i);
}
}

}

Output
Executed by Servant1 and output: 0
Executed by Srevant2 and output: 0
Executed by Servant1 and output: 1
Executed by Srevant2 and output: 1
Executed by Servant1 and output: 2
Executed by Srevant2 and output: 2
Executed by Servant1 and output: 3
Executed by Servant1 and output: 4
Executed by Srevant2 and output: 3
Executed by Srevant2 and output: 4

Explanation:

  1. ThreadB implements Runnable interface, and it overrides the run method

  2. In the main method, instance of ThreadB is created which is of type runnable

  3. Create a thread instance and pass the runnable object inside the thread. [here the Thread(Runnable target) constructor is used]

  4. So the advantage here is, we can pass the job of one runnable object to multiple threads as given above. As you can see in the above example, servant1 and servant2 are 2 threads, which takes the runnable object as their constructor parameter. When we call run on each thread, each executes the same code inside run method separately.



Thread Constructors

  1. Thread() - Creates thread

  2. Thread(Runnable Target) - creates thread with the runnable object

  3. Thread (Runnable Target, String name) - Creates thread with the runnable object and sets the name to the thread

  4. Thread(name) - Creates a thread and sets the name to the thread


Important points:

  1. Once the run method finishes thread reaches the dead state.

  2. Calling start method on thread instance should be only once for the thread's life time. Second call of start method on the same thread causes the IllegalThreadStateException.

  3. The order of thread execution is not guaranteed always. That means the above order of thread execution will change for each execution. If you are not seeing the order change, then increase the loop to 400 and see.

No comments:

Post a Comment