1) What is Multithreading in Java ?
It is a process of executing multiple threads simultaneously.
It is used to achieve multitasking.
It is mostly used in animation and games etc.
2) What are the advantages of Java Multithreading?
Advantages of multithreading are:
- Saves time
- Threads are independent
- It doesn't affect other threads if exception occur in a single thread.
- You can perform multiple operations at same time.
3) What is the use of Thread.start() method in Java?
Thread.start() method is used to run the Thread.run() method in a thread.
Thread.start() method is also known as native method.
4) What are different states in lifecycle of Thread?
Different states in lifecycle of thread are:
- New
- Runnable
- Running
- Non-Runnable
- Terminated
5) How can we create a Thread in Java?
Two ways to create thread in java are:
- By extending Thread class
- By implementing Runnable interface.
6) What are the commonly used constructors of thread class in Java?
The commonly used Constructors of thread class are:
- Thread()
- Thread(String name)
- Thread(Runnable r)
- Thread(Runnable r,String name)
7) What are the commonly used methods of thread class in Java?
The commonly used methods of thread class in java are:
- public void run()
- public void start()
- public void sleep(long miliseconds)
- public void join()
- public void join(long miliseconds)
- public int getPriority()
- public int setPriority(int priority)
- public String getName()
- public void setName(String name)
- public Thread currentThread()
- public int getId()
- public Thread.State getState()
- public boolean isAlive()
- public void yield()
- public void suspend()
- public void resume()
- public void stop()
- public boolean isDaemon()
- public void setDaemon(boolean b)
- public void interrupt()
- public boolean isInterrupted()
- public static boolean interrupted()
8) How can we create a thread by extending thread class in Java?
Example:
class TutorialandExample1 extends Thread
{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[])
{
TutorialandExample1 t1=new TutorialandExample1();
t1.start();
}
}
9) How can we create a thread by implementing Runnable interface in Java?
Example:
class TutorialandExample2 implements Runnable
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[]){
TutorialandExample2 m1=new TutorialandExample2();
Thread t1 =new Thread(m1);
t1.start();
}
}
10) How can we start a thread twice in Java?
Example:
public class TutorialandExample3 extends Thread{
public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TutorialandExample3 t1=new TutorialandExample3();
t1.start();
t1.start();
}
}
11) What is the use of currentThread() method in Java?
It is used to returns a reference to the currently executing thread object.
class TutorialandExample4 extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[]){
TutorialandExample4 t1=new TutorialandExample4();
TutorialandExample4 t2=new TutorialandExample4();
t1.start();
t2.start();
}
}
12) How threads can communicate with each other?
Threads can communicate with each other by using:
wait() method
notify() method
notifyAll() method
13) Is it important to acquire object lock before calling wait(), notify() and notifyAll()?
Yes, It is important to acquire object lock before calling these methods on object.
14) How can we create a Daemon thread in Java ?
Example:
public class TutorialandExample5 extends Thread {
public void run(){
if(Thread.currentThread().isDaemon())
{
System.out.println("daemon thread work");
}
else
{
System.out.println("user thread work");
}
}
public static void main(String[] args){
TutorialandExample5 t1=new TutorialandExample5();
TutorialandExample5 t2=new TutorialandExample5();
TutorialandExample5 t3=new TutorialandExample5();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}
15) What are the important methods of ThreadGroup class in Java?
Methods of ThreadGroup class in java are:
- int activeCount()
- int activeGroupCount()
- void destroy()
- String getName()
- ThreadGroup getParent()
- void interrupt()
- void list()