HTML Java

Java Multitasking Interview Questions


Ques: What is Multitasking?

Sol: Suppose you are writing document in Ms-word in your laptop at sametime you are listening music is called multitasking.

And when you are typing in word spell check also checks your grammatical mistake, that is multithreading.


Ques: What is the difference between process and thread?

Sol: Process is the heavy weight entity and thread is lightweight. Thread is lightweight because it has no address and share address of process and process has a separate address space in a memory.

Context switching between the process is more and context switching between threads is less.

Each process requires resources separately and thread doesn’t require separate resources and its share the same resources.


Example: Suppose, 4 friends decides to hangout in Chandigarh and all of them booked a car so they can start their journey when they were on the way all of them felt hungry and everyone wants to eat something different like lassi, burger, coffee and pasta. So if they stop the car at 1 place where they can only get lassi and not the rest of the things, then after finishing one they again needs to stop at some other place to have rest thing so it will take more time. Rather than stopping at different places they should find a place where everyone can get their favorite food. This way they can save time and can reach Chandigarh on time. so its all about example of process now example of Thread suppose they stopped their car at 1 place where they got every kind of food, so they ordered at a time, now when cook start preparing pasta all the vegetable get fry on pan and the same time he should put pasta in bowl on stove to cook and while veg are frying n pan he can grate cheese as well so it can save time so it’s an example of multithreading. If he fries first then cook pasta then grate cheese then efficiency will decrease.

We use multithreading to develop software’s. And multiprocessing used by OS. So that it can achieve process based multitasking so there is some Drawbacks so that’s why we use thread based multitasking in application software. JVM is a software Program when this program in execution state it become a process.

Inside java.lang.Thread

Constructor inside thread Class

  • public Thread()
  • public Thread (String name)
  • public Thread(Runnable r)
  • public Thread(Runnable r, String name)

They all are commonly used constructor.


Commonly used method of Thread Class

  • public void start() - to start thread
  • public void run() - to assign a task inside a thread
  • public String getName() -get current thread name
  • public Thread CurrentThread() - get current exceuting thread object
  • public void setPriority(int priority) –to set priority of any thread
  • public int getPriority() -get Priority of any thread
  • public void Sleep(int time) -
  • public void setDaemon(boolean b)
  • public Boolean isDaemon()
  • public void yield(), etc.

Ques: How to create a multithreading program in java?

Sol: By Extending Thread(Inheritance)

by Implementing Runnable interface(Association)

Note: Thread class implement runnable interface internally


by inheritance

Steps: Creating a class and which extends a thread class now override the run method and assign a task in a thread class. And create an object of a the class which extends and start the thread.

Example of multi thread and Single Task

import java.lang.Thread;
class DemoThread extends Thread{
public void run(){
System.out.println("Hello Thread");
}
public static void main(String args[]){
DemoThread d = new DemoThread();
d.start();
d.start();
DemoThread d1 = new DemoThread();
D1.setName(“hi”);
d1.start();
} }

Output:

Note: When a thread starts then it can’t restart.
Don’t think that an object of created then thread start until we not start thread. If thread started then automatically run method will call. And for every thread each stack is maintain in the memory. And thread has separate part of execution.


Ques: When Threads Dead?

Sol: When the task assigned to the thread is completed. One thread is dead then it can’t restart.

When you call start method twice on a thread then you will get an IllegalStateException.


Ques: Can we call run method forcefully?

Sol: Yes but thread will not run because at that time run method will work as a normal method. Run method executes when thread start.

When we create a thread and we didn’t give name to it then JVM assigns some name to thread like thread 0, thread 1 …. If you want to set thread name then you do this thing before starting thread.

2 ways to give name to a thread by using setName method or we can create constructor of a class and call super constructor because Thread class having constructor we call that constructor from our class constructor by using super keyword and value initialize in thread class and when we call getName method then we get name of thread like this example.

Example

import java.lang.Thread;
class DemoThread extends Thread{

DemoThread(String n){
super(n);
}
public void run(){
System.out.println("Hello Thread");
}
public static void main(String args[]){
DemoThread d = new DemoThread("d");
d.start();
System.out.println("Thread Name of d ="+d.getName());
DemoThread d1 = new DemoThread("d1");
d1.start();
System.out.println("Thread Name of d ="+d1.getName());
}
}

Ques: Program of multi thread multitask.

Sol:

Example

import java.lang.Thread;
class A extends Thread{
A(String n){
super(n);
}
public void run(){
System.out.println("Hi A");
}
}

class B extends Thread{
B(String n){
super(n);
}
public void run(){
System.out.println("Hi B ");
}
}

class DemoThread extends Thread{
public static void main(String args[]){
A a = new A("A1");
a.start();
System.out.println("Thread Name of a ="+a.getName());
B b = new B("B1");
b.start();
System.out.println("Thread Name of b ="+b.getName());
}
}

In multithreading there is no sequence of execution of threads by default but for maintain sequence we need to learn synchronizing. Its not matter that if we set priority to thread then it will run first. Ans:” no” which thread will run 1st decided by thread scheduler.