Threads( single tasking and multi tasking) - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Threads( single tasking and multi tasking)

Threads( single tasking and multi tasking)

Threads

A  thread represent a separate path of execution of a group of statement In java program . 
if we write a group of statements. then these statement are executed by JVM one by one .
this execution is called a thread. 





How to find running thread in this program

class RunningThreads
{
     public static void main(String[]arr){
    System.out.println("find the running current thread");
     Thread th=new Thread.currentThread();
       System.out.println("current thread="+th);
        System.out.println("That is thread name="+th.getName);
       }
   }

Output









* which thread always runs in java program by default?
Mian thread

Why threads are called light weight?
Thread are light weight because they utilize minimum resources of the system.
This means they take less memory and less processor time.

What is difference between single tasking and multi tasking?
Executing only one job at a time is called single tasking.Executing several jobs at a time is 
called multi tasking . in single tasking .the processor time is wasted , but in multi tasking we
can utilize processor time in a optimum way. 


How can  you stop a thread in java ?
First of all we should create  a boolean type variable which store 'false' When the user wants 
to stop the thread, we should store 'true' into the variable The status of the variable is checked in the run() methods and if it is true the thread executes 'retuen' statement and then stop.

Example As like 

........ // thread work here two way
//run() mode and stop()

import java.io.*;
class MyThread extends Thread

    boolean stop =false;                                                                           OUTPUT
    public void run() {
          for(int i=1; i<=1000; i++)
          {
             System.out.println(i);
             if(stop)
          return; }
      }
}

class Demo1
{
  public static void main(String[] args)throws IOException {
    MyThread obj=new MyThread();
    Thread t=new Thread(obj);
    t.start();
    System.in.read();// stop the thread when pressed the enter key 
    obj.stop=true;
  }
}

About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.