Tuesday, 25 April 2017

3. Create a thread using Thread class

1. How to create a thread using Thread class
  • In java, if we want to create a thread by using Thread class, we have 2 ways
  • Way : 1
    1. Create a custom class with extends Thread class.
    2. Override run() from Thread class.
    3. Write business logic in run().
    4. Create an object for custom class(which extends Thread class).
    5. Call start() through the created object(start() will inherit from Thread class, no need to override).

public class ThreadDemo extends Thread{ //a

@Override
public void run() { //b
for(int i=0; i<10; i++){//c
System.out.println("i am in run()");
}
System.out.println("end of run()");
}
public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();//d
t1.start();//e
System.out.println("end of main()");
}

}


  • Way : 2
    1. Create a custom class with extends Thread class.
    2. Override run() from Thread class.
    3. Write business logic in run().
    4. Create an object for custom class(which extends Thread class).
    5. Creates an object for Thread class, and pass above created object(which extends Thread class).
    6. Call start() through the thread object.

public class ThreadDemo extends Thread{ //a

@Override
public void run() { //b
for(int i=0; i<10; i++){//c
System.out.println("i am in run()");
}
System.out.println("end of run()");
}
public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();//d
Thread thread = new Thread(t1);//e
thread.start();//f
System.out.println("end of main()");
}

}


2. What happen if we not override run() from Thread class
·         In Thread class implements Runnable interface, it overrides run() without any logic.
·         So if we call Thread class start(), without overriding run(), thread will be created and run without any logic
public class ThreadDemo extends Thread{

public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();
t1.start();
System.out.println("end of main()");
}

}


public class ThreadDemo extends Thread{

public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();
Thread thread = new Thread(t1);
thread.start();
System.out.println("end of main()");
}

}


·         In above examples, thread is created, and also started its execution, but no logic is available to execute.
·         So thread will completes its execution immediately(because no logic is there in run() of Thread class).


3. What is the internal logic of start() in Thread class
  • start() will causes the Thread to begin execution.
  • start() will not call run() directly.
  • start() will call start0() it is native method, after calling this method thread will return to our program.
  • start0() is native method so jvm creates a thread in that native method.
  • JVM calls the run() of the newly created thread.
  • It is never legal to start a thread more than once.
  • If we call more than once it throws exception.(IllegalThreadStateException)
  • If thread is completed its execution, than thread will goes to dead state, but thread object may available, but it is not a thread, we can’t call start() after completes thread also.
  • Seel java code for Thread class ‘this’


4.   Can we override start() of Thread class

  • Yes, we can override but not recommended because we can’t write thread creation logic.
  • So don’t override start()



No comments:

Post a Comment

3. Java Program to create Binary Tree