Friday 28 April 2017

6.Thread basics


1. Where we can use multi thread in real time.
·         On development of servers like application servers and web servers, because each client request is switch to a thread.
·         Each independent task implementation.
·         When compared with old languages developing multi threaded applications in java very easy because java provides in built support for multithreading with rich api (Thread, Runnable,ThreadGroup ....).
2. What is the job of thread.
·         The code written in the run() is called job of the thread.
·         The logic which will executed by the thread is called as JOB.
·         We can define a JOB inside run()
3. How threads will be executed.
  1. Thread Scheduler is the part of JVM
  2. It is the responsible to schedule threads that is multiple threads are waiting to get the chance of execution then in which order threads will be executed is decided by Thread Scheduler.
  3. If decides which thread executes first, it decides the order of executions of all threads
  4. Thread Scheduler algorithm is different from JVM to JVM and also OS
    1. FCFS
    2. SJF
    3. Round Robin
  5. We can’t expect exact threads execution order and output order if multiple threads execution.
Note : If there is no guarantee in the output order, how can we use multi threads in java. Because of all threads must be independent tasks, so need of particular order of output.


4. t.start() vs t.run()
1.       t.start() will create new Thread and run() code will execute by new  thread.
2.       t.run() will run run() code by the current thread. It will not create Thread.
5. Can we overload run()
1.       We can also overload run() in our class, but it will invoke default run() override from Thread class.
public class MyThread extends Thread {
  @Override
  public void run() {
        System.out.println("No args run()");
  }
  public void run(int 1) {
        System.out.println("parameters run(i)");
  }
}
O/P : No args run()
2.       Thread class start() always invoke no params run().
6. Overriding of start()
1.       If we override start(), then our star() will be executed just like normal method call and new won’t be created.
public class MyThread extends Thread {
  @Override
  public void start() {
        System.out.println("start method");
  }
  @Override
  public void run() {
        System.out.println("run method");
  }
  public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
  }
}
O/P : start method
Note : It is highly not recommended to override start().


7. Overriding of start() with calling super.start()


1.       If we override start(), by calling super.start(), then our star() will be executed by main thread and new thread will be created and it executes run().
public class MyThread extends Thread {
  @Override
  public void start() {
        super.start();
        System.out.println("start method");
  }
  @Override
  public void run() {
        System.out.println("run method");
  }
  public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
  }
}
O/P : run method , start method (or) start method, run method





No comments:

Post a Comment

3. Java Program to create Binary Tree