Java Multi-Threading

  Home  Java Programing  Java Multi-Threading


“Java multi-threading frequently Asked Questions by expert members with experience in Java Multi-Threading. These interview questions and answers on Java multi-threading will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. So get preparation for the Java multi-threading job interview”



36 Java Multi Threading Questions And Answers

21⟩ What is Thread leak?

Thread leak is when application does not release references of the thread object and those threads do not get garbage collected.

Number of such unused threads increases with time and it can cause issues in the application like long response time.

To overcome this problem we can do the following

1. By maintaining a log for all entry and exit point of thread.

2. Check how the new thread is created and how it is closed.

3. By using exception handling etc.

 201 views

23⟩ Explain the method of Thread class with example?

In this method of creating thread, we have to extend the Thread class and override the run() method in our class to create a Thread.

We have to create an object of the our class.

Once the object is created then we have to invoke the start() method and it will generate a new thread of execution.

For example

public class MyThread extends Thread{

public void run(){

// code to execute under the thread

}

public static void main(String [] args){

MyThread c = new MyThread();

c.start();

}

}

 151 views

24⟩ Explain the method of Runnable interface with example?

In this method of creating thread, we have to implement the Runnable interface and implement the run() method in our class.

We have to create an object of our class.

Then we you have to pass the reference of that object for creating a new object of Thread

Invoke the start method using this Thread object which will create a new thread of execution.

For example

public class MyThread implements Runnable{

public void run(){

// code to execute under the thread

}

public static void main(String [] args){

MyThread c = new NewThread();

Thread t = new Thread(c);

t.start();

}

}

 160 views

26⟩ What are the methods of the thread class used to schedule the threads?

The methods of the thread class used to schedule the threads are as follows:

-public final void join() throws InterruptedException

-public final void notify()

-public final void notifyAll()

-public static void yield()

-public final void setPriority(int priority)

-public static void sleep(long millis) throws InterruptedException

-public final void wait() throws InterruptedException

 163 views

27⟩ Explain the difference between preemptive scheduling and time slicing?

In Preemptive scheduling, highest priority task will executes until it enters in waiting or dead states. It also executes, until a higher priority task enters.

In Time slicing, a task will execute for a fixed time slice and after that it will go in ready state.

At that time the scheduler will find the executable task, according to the priority and various other tasks.

In preemptive scheduling, the running task will be preempted by the higher priority task.

In time slicing methods, a task executes until the specified period of time. Once the execution of that task is complete then the higher priority task will be executed if available.

 151 views

28⟩ What the difference is between notify and notify All methods?

The notify will run only first thread which has called wait() on same object. i.e. the object which has called the wait() must be the same as the object that calls

The notifyAll will run all threads which has called wait() on same object.

While using notifyAll the highest priority thread will run first.

 172 views

29⟩ What is thread synchronization?

In multithreading, the shared resources are allocated to one thread at a time.

multiple thread ensuring that the shared resources will be allocated to only one thread at a time is called thread synchronization.

The synchronized keyword is used to define the critical block in which every Java object get a lock to enter.

Syntax:

Synchronized(object) // object is reference of the object to be synchronized

{

// statement to be synchronized.

}

The synchronized keyword provides locking which maintain the mutual exclusive access of shared resource and race of objects for data.

 163 views

30⟩ Explain the difference between yielding and sleeping?

Sleep causes the currently executing thread to sleep until the specified time is completed. The thread will resume once the specified time period is over.

Sleep causes the currently executing thread to sleep and gives a chance to other threads to execute. The thread will join the ready queue.

Thread.sleep() will moves the thread to “Wait” state.

Thread.yield() will moves the thread to “Ready” state.

 173 views

31⟩ Explain some ways in which a thread can enter the waiting state?

A thread can enter the waiting state by the following ways:

We can invoke sleep() method of the thread.

An attempt to acquire the object’s lock can put the thread in waiting mode.

We can also invoke wait() method of the thread.

A thread can also be entered in waiting state by invoking its suspend() method.

 160 views

32⟩ Explain Thread?

We can describe "thread" in two ways in Java.

1. An instance of class java.lang.Thread.

2. A thread of execution.

An instance of Thread is an object like all other objects in Java, it also has variables and methods and it lives and dies on the heap.

But a thread of execution is an individual light weight process that has its own call stack.

Even if you don't create any new threads in your program, threads are there running in background.

The main() method runs the main thread. So in main call stack the main() method would be the first.

When a new thread is created then it will have its own stack separate from the main() call stack.

 164 views

33⟩ What is starvation?

Starvation is a situation when some threads acquired the shared resources for long time and therefore other threads are not able to access those resources and not able to do anything further.

For example, suppose an object provides a synchronized method that often takes a long time to return.

If one thread invokes this method frequently, other threads that also require frequent synchronized access to that object will be blocked.

In Java, Starvation can be caused by inappropriate allocation of thread priorities.

A thread with low priority can be starved by the threads of higher priority if the higher priority threads do not release shared resources time to time.

 163 views

34⟩ Explain deadlock?

When two or more threads are waiting for each other and get blocked forever then this situation is called Deadlock.

During deadlock two threads, each thread has acquired a lock on one resource and trying to acquire a lock on resource of other thread.

Each thread will wait indefinitely for the other to release the lock, unless one of the user processes is stopped.

In terms of Java API, thread deadlock situation can arise in following conditions:

1. When two threads call Thread.join() on each other.

2. When two threads use nested synchronized blocks to lock two objects and the blocks lock the same objects in different order.

 152 views

35⟩ What is thread Local variable?

If a variable is declared as threadLocal then all the thread which accesses that variable will maintain its own copy of variable and would not use the copy of any other thread.

For example let consider the scenario of giving JDBC connection to each thread.

public class ThreadLocal {

public Object get();

public void set(Object myValue);

public Object FirstValue();

}

Implementation of ThreadLocal

public class ConnectionDispenser {

private static class ThreadLocalConnection extends ThreadLocal {

public Object FirstValue () {

return DriverManager.getConnection(ConfigurationSingleton.getDbUrl());

}

}

private static ThreadLocalConnection con = new ThreadLocalConnection();

public static Connection MyConnection() {

return (Connection) con.get();

}

}

 180 views

36⟩ How to debug our application for issues when multiple threads are being executed?

Following are some way to debug issues in multi-threaded applications in Java.

-By using logging and print statements along with thread names. In this way we can know about the flow of thread execution.

-With the use of debugging functionality available in Eclipse and JDeveloper.

-We can write a thread dump of the application which will give the information about the active threads at a point of time.

This is most effective way for detecting deadlocks in production systems.

 188 views