Core Java Interview Questions | Eklavya Online

Core Java Interview Questions

The following code snippet changes the Counter class to maintain individual counting as in each user counter will be incremented starting from 1.

The following code snippet changes the Counter class to maintain individual counting as in each user counter will be incremented starting from 1. So, the Counter will no longer be the shared resource. The CountingTask class is also modified to loop through each user 2 times as shown below. Is there anything wrong with the …

The following code snippet changes the Counter class to maintain individual counting as in each user counter will be incremented starting from 1. Read More »

How will you fix the above racing issue

This can be fixed a number of ways. Option 1: Method level synchronization. This is the simplest. As you can see, the increment() method is synchronized, so that the other threads must wait for the thread that already has the lock to execute that method. import java.util.HashMap; import java.util.Map;   public class Counter {   //shared …

How will you fix the above racing issue Read More »

Explain how you would get thread-safety issues due to non-atomic operations with a code example

The code snippets below demonstrates non-atomic operations producing incorrect results with code. The program below uses a shared Counter object, that is shared between three concurrent users (i.e. three threads). The Counter object is responsible for incrementing the counter. Firstly, the Counter class. The counted values are stored in a HashMap by name (i.e. thread name) as the key for …

Explain how you would get thread-safety issues due to non-atomic operations with a code example Read More »

Can you have a true singleton class in Java? How would you write a thread-safe singleton class

A singleton class is something for which only one instance exists per class loader. Single instance for a wholeapplication cannot be guaranteed. That is just definition of what is singleton. The one that is  popular with the interviewers is writing a thread-safe singleton class. For example, the following singleton class is not thread-safe because before a thread creates …

Can you have a true singleton class in Java? How would you write a thread-safe singleton class Read More »

Can you give some examples of thread racing conditions you had experienced

Declaring variables in JSP pages are not thread-safe. The declared variables in JSP pages end-up as instance variables in the converted Servlets. <%! Calendar c = Calendar.getInstance(); %> Decalring instance variables in Servlets is not thread safe, as Servlets are inherently multi-threaded and gets accessed by multiple-threads. Same is true for the Actionclasses in the …

Can you give some examples of thread racing conditions you had experienced Read More »

Write a multi-threaded Java program in which, one thread generates odd numbers and write to a pipe and the second thread generates even numbers and write to another pipe, and a third thread receives the numbers from both the pipes and evaluates if the sum is multiples of 5

In Unix, a pipe (“|”) operator helps you to redirect output from one command to another. PipedReader and PipedWriterclasses in java.io package helps you to do the same. It helps you to redirect the read input into writer seamlessly. In Unix, two different processes on different address spaces can communicate using pipe, but in java two threads …

Write a multi-threaded Java program in which, one thread generates odd numbers and write to a pipe and the second thread generates even numbers and write to another pipe, and a third thread receives the numbers from both the pipes and evaluates if the sum is multiples of 5 Read More »

Can you write a program with 2 threads, in which one prints odd numbers and the other prints even numbers up to

In Java, you can use wait(  ) and notifyAll(  ) to communicate between threads. The code below demonstrates that:   Note: This a typical example of splitting tasks among threads. A method calls notify/notifyAll( ) as the last thing it does (besides return). Since the printOdd( ) and printEven( ) methods were void, the notifyAll( ) was the last statement. If …

Can you write a program with 2 threads, in which one prints odd numbers and the other prints even numbers up to Read More »

Explain how you would get a Thread Deadlock with a code example

The example below causesa deadlocksituation bythread-1 waiting for lock2and thread-0 waiting for lock1.   package deadlock;   public class DeadlockTest extends Thread { public static Object lock1 = new Object(); public static Object lock2 = new Object(); public void method1() { synchronized (lock1) { delay(500);  //some operation System.out.println(“method1: ” + Thread.currentThread().getName()); synchronized (lock2) { System.out.println(“method1 …

Explain how you would get a Thread Deadlock with a code example Read More »

How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?What is the difference between synchronized method and synchronized block

In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in method level (coarse grained lock – can affect performance adversely) or block level of code (fine grained lock). Often using a lock on a method level …

How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?What is the difference between synchronized method and synchronized block Read More »