Thursday, May 23, 2019

Java Constructor & Types of Constructor in Java with examples

1. What is a Constructor in Java?

Java constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.

Java constructor interview questions

2. What are the Rules for defining a constructor?

  1. Constructor name should be the same as the class name
  2. It cannot contain any return type
  3. It can have all Access Modifiers are allowed (private, public, protected, default)
  4. It Cannot have any Non Access Modifiers (final, static, abstract, synchronized)
  5. No return statement is allowed
  6. It can take any number of parameters
  7. A constructor can throw an exception, we can have a throws clause

3. What is the use of Private Constructors in Java?

When we use privately for a constructor then object for the class can only be created internally within the class, no outside class can create an object for this class. Using this we can restrict the caller from creating objects.
class PrivateConstructorExample
{
    /**
     * Private Constructor for preventing object creation
    from outside class
    **/
    private PrivateConstructorExample(){ }
    
    public void disp()
    {
        System.out.println("disp() method called");
    }
}
public class Sample 
{
    public static void main(String args[])
    {
        //Creating the object for the Private Constructor class
        PrivateConstructorExample pc = new PrivateConstructorExample();
        
        pc.disp();
    }
}
When we run the above code we will be getting the below exception.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 The constructor PrivateConstructorExample() is not visible

 at Sample.main(Sample.java:19)

4. Can we have a Constructor in an Interface?

No, We cannot have a Constructor defined in an Interface.

Java constructor interview questions

5. What is Constructor Chaining in Java?

Constructor Chaining is nothing but calling one Constructor from another. this keyword is used to call the current class constructor and the super keyword is used to call the parent class constructor.
class Parent
{
    public Parent()
    {
        System.out.println("Parent class no-args constructor called");
    }
    public Parent(String name)
    {
        System.out.println("Parent class Parameterized constructor called by "+name);
    }
}
public class Child extends Parent
{
    public Child()
    {
        this("JIP");
        System.out.println("Child class no-args constructor called");
    }
    public Child(String name)
    {
        super("JIP");
        System.out.println("Child class Parameterized constructor called by "+name);
    }
    public static void main(String args[])
    {
        Child c = new Child();
    }
}
Output :
Parent class Parameterized constructor called by JIP
Child class Parameterized constructor called by JIP
Child class no-args constructor called

6. Can we have this and super in the same constructor?

No, we cannot have this and super in the same constructor as anyone only can be in the first line of the constructor.
class Parent
{
    public Parent()
    {
        System.out.println("Parent class no-args constructor");
    }
}
public class Child extends Parent
{
    public Child()
    {
        this("JIP");
        super();
        System.out.println("Child class no-args constructor");
    }
    public Child(String name)
    {
        
        System.out.println("Child class Parameterized constructor"+name);
    }
    public static void main(String args[])
    {
        Child c = new Child();
    }
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 Constructor call must be the first statement in a constructor

 at Child.(Child.java:13)
 at Child.main(Child.java:23)

Java constructor interview questions

7. Is it possible to call a subclass constructor from superclass constructor?

No. You cannot call a subclass constructor from a superclass constructor.

8. What is a No-arg constructor?

Constructor without arguments is called the no-arg constructor. In Java Default constructor is a no-arg constructor.
class Demo
{
    public Demo()
    {
        //No-arg constructor
    }
}

9. Can we have a class with no Constructor in it? What will happen during object creation?

Yes, we can have a class with no constructor, When the compiler encounters a class with no constructor then it will automatically create a default constructor for you.

10. Can we have both Default Constructor and Parameterized Constructor in the same class?

Yes, we have both Default Constructor and Parameterized Constructor in the same class.

Java constructor interview questions

11. Can a Constructor return any value?

A Constructor cannot return any explicit value but implicitly it will be returning the instance of the class.

12. Will compiler create the Default Constructor when we already have a Constructor defined in the class?

No, the compiler will not create the Default Constructor when we already have a Constructor defined.

13. Can an abstract class in Java have a constructor?

Yes, an abstract class can have a constructor. The below code works perfectly fine.
abstract class Demo1 { 
    String value;
    public Demo1( String value ) {
        this.value = value;
    }
    public String getValue()
    {
     return value;
    }
}
public class Test extends Demo1 {
    public Test() {
        super("CoreJava");
    }
}

Java constructor interview questions

14. What happens when a Constructor is defined as “protected”?

In general protected method can be accessed by other class in a different package only through Inheritance. But when you assign protected access to a constructor it behaves a bit different. It can be accessed only by a call of super() (according to JLS) and not directly by any other means.
package com.javainterviewpoint;

public class Parent
{
    protected Parent()
    {
        System.out.println("Parent Constructor called");
    }
    public void parentDisp()
    {
        System.out.println("Parent Disp called");
    }
}
package com.javainterviewpoint1;

import com.javainterviewpoint.Parent;

public class Child extends Parent
{
    public Child()
    {
        /**
         * Using super() Parent Class protected constructor can be called
         */
        super(); 
        System.out.println("Child Constructor called");
    }
    public void childDisp()
    {
        System.out.println("Child Disp called");
    }
    public static void main(String args[])
    {
        /**
         * Even though we have extended Parent class in Child class, 
         * below way of calling Parent class Constructor is not allowed
         * 
         * The constructor Parent() is not visible - error will be thrown
         */
        Parent p = new Parent() // Error will be thrown
    }
}

15. Why constructors cannot be final in Java?

When you set a method as final, then” The method cannot be overridden by any class”, but Constructor by JLS ( Java Language Specification ) definition can’t be overridden. A constructor is not inherited, so there is no need for declaring it as final.

16. Why constructors cannot be abstract in Java?

When you set a method as abstract, then “The method doesn’t or cannot have the body”. A constructor will be automatically called when the object is created. It cannot lack a body moreover an abstract constructor could never be implemented.

17. Why constructors cannot be static in Java?

When you set a method as static, it means “The Method belongs to a class and not to any particular object” but a constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.
Also Read: Java Interview Questions class and object in Java Java Constructor Wells Fargo Routing Number Bank of America Routing Number

Java Multithreading Interview Questions and Answers

Java Multithreading Interview Questions and Answers

1) What is multithreading?

Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. It consumes less memory and gives a fast and efficient performance. Its main advantages are:
  • Threads share the same address space.
  • The thread is lightweight.
  • The cost of communication between the processes is low.

2) What is the thread?

A thread is a lightweight subprocess. It is a separate path of execution because each thread runs in a different stack frame. A process may contain multiple threads. Threads share the process resources, but still, they execute independently.

3) Differentiate between process and thread?

There are the following differences between the process and thread.
  • A Program in the execution is called the process whereas; A thread is a subset of the process
  • Processes are independent whereas threads are the subset of process.
  • The process have different address space in memory, while threads contain a shared address space.
  • Context switching can be faster between the threads as compared to context switching between the threads.
  • Inter-process communication is slower and expensive than inter-thread communication.
  • Any change in Parent process doesn't affect the child process whereas changes in parent thread can affect the child thread.

Java Multithreading Interview Questions

4) What do you understand by inter-thread communication?

  • The process of communication between synchronized threads is termed as inter-thread communication.
  • Inter-thread communication is used to avoid thread polling in Java.
  • The thread is paused running in its critical section, and another thread is allowed to enter (or lock) in the same critical section to be executed.
  • It can be obtained by wait(), notify(), and notifyAll() methods.

5) What is the purpose of wait() method in Java?

The wait() method is provided by the Object class in Java. This method is used for inter-thread communication in Java. The java.lang.Object.wait() is used to pause the current thread, and wait until another thread does not call the notify() or notifyAll() method. Its syntax is given below.
public final void wait()

6) Why must wait() method be called from the synchronized block?

We must call the wait method otherwise it will throw java.lang.IllegalMonitorStateException exception. Moreover, we need wait() method for inter-thread communication with notify() and notifyAll(). Therefore It must be present in the synchronized block for the proper and correct communication.

7) What are the advantages of multithreading?

Multithreading programming has the following advantages:
  • Multithreading allows an application/program to be always reactive for input, even already running with some background tasks
  • Multithreading allows the faster execution of tasks, as threads execute independently.
  • Multithreading provides better utilization of cache memory as threads share the common memory resources.
  • Multithreading reduces the number of the required server as one server can execute multiple threads at a time.

8) What are the states in the lifecycle of a Thread?

A thread can have one of the following states during its lifetime:
  1. New: In this state, a Thread class object is created using a new operator, but the thread is not alive. Thread doesn't start until we call the start() method.
  2. Runnable: In this state, the thread is ready to run after calling the start() method. However, the thread is not yet selected by the thread scheduler.
  3. Running: In this state, the thread scheduler picks the thread from the ready state, and the thread is running.
  4. Waiting/Blocked: In this state, a thread is not running but still alive, or it is waiting for the other thread to finish.
  5. Dead/Terminated: A thread is in the terminated or dead state when the run() method exits.

Java Multithreading Interview Questions

9) What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

10) What is context switching?

In Context switching the state of the process (or thread) is stored so that it can be restored and execution can be resumed from the same point later. Context switching enables multiple processes to share the same CPU.

11) Differentiate between the Thread class and Runnable interface for creating a Thread?

The thread can be created by using two ways.
  • By extending the Thread class
  • By implementing the Thread class
However, the primary differences between both the ways are given below:
  • By extending the Thread class, we cannot extend any other class, as Java does not allow multiple inheritances while implementing the Runnable interface; we can also extend other base class(if required).
  • By extending the Thread class, each of thread creates the unique object and associates with it while implementing the Runnable interface; multiple threads share the same object
  • Thread class provides various inbuilt methods such as getPriority(), isAlive and many more while the Runnable interface provides a single method, i.e., run().

Java Multithreading Interview Questions

12) What does the join() method?

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. Join method is overloaded in Thread class in the following ways.
  • public void join()throws InterruptedException.
  • public void join(long milliseconds)throws InterruptedException.

13) Describe the purpose and working of sleep() method.

The sleep() method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time. There are two methods of doing so.
Syntax:
  • public static void sleep(long milliseconds)throws InterruptedException
  • public static void sleep(long milliseconds, int nanos)throws InterruptedException
Working of sleep() method
When we call the sleep() method, it pauses the execution of the current thread for the given time and gives priority to another thread(if available). Moreover, when the waiting time completed then again previous thread changes its state from waiting to runnable and comes in running state, and the whole process works so on till the execution doesn't complete.

14) What is the difference between wait() and sleep() method?

wait()sleep()
1) The wait() method is defined in Object class.The sleep() method is defined in Thread class.
2) The wait() method releases the lock.The sleep() method doesn't release the lock.


Java Multithreading Interview Questions

15) Is it possible to start a thread twice?

No, we cannot restart the thread, as once a thread started and executed, it goes to the Dead state. Therefore, if we try to start a thread twice, it will give a runtimeException "java.lang.IllegalThreadStateException". Consider the following example.
  1. public class Multithread1 extends Thread  
  2. {  
  3.    public void run()  
  4.     {  
  5.       try {  
  6.           System.out.println("thread is executing now........");  
  7.       } catch(Exception e) {  
  8.       }   
  9.     }  
  10.     public static void main (String[] args) {  
  11.         Multithread1 m1= new Multithread1();  
  12.         m1.start();  
  13.         m1.start();  
  14.     }  
  15. }  
Output
thread is executing now........
Exception in thread "main" java.lang.IllegalThreadStateException
 at java.lang.Thread.start(Thread.java:708)
 at Multithread1.main(Multithread1.java:13)

16) Can we call the run() method instead of start()?

Yes, calling run() method directly is valid, but it will not work as a thread instead it will work as a normal object. There will not be context-switching between the threads. When we call the start() method, it internally calls the run() method, which creates a new stack for a thread while directly calling the run() will not create a new stack.

17) What about the daemon threads?

The daemon threads are the low priority threads that provide the background support and services to the user threads. Daemon thread gets automatically terminated by the JVM if the program remains with the daemon thread only, and all other user threads are ended/died. There are two methods for daemon thread available in the Thread class:
  • public void setDaemon(boolean status): It used to mark the thread daemon thread or a user thread.
  • public boolean isDaemon(): It checks the thread is daemon or not.

18)Can we make the user thread as daemon thread if the thread is started?

No, if you do so, it will throw IllegalThreadStateException. Therefore, we can only create a daemon thread before starting the thread.
  1. class Testdaemon1 extends Thread{    
  2. public void run(){  
  3.           System.out.println("Running thread is daemon...");  
  4. }  
  5. public static void main (String[] args) {  
  6.       Testdaemon1 td= new Testdaemon1();  
  7.       td.start();  
  8.       setDaemon(true);// It will throw the exception: td.   
  9.    }  
  10. }  
Output
Running thread is daemon...
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1359)
at Testdaemon1.main(Testdaemon1.java:8

19)What is shutdown hook?

The shutdown hook is a thread that is invoked implicitly before JVM shuts down. So we can use it to perform clean up the resource or save the state when JVM shuts down normally or abruptly. We can add shutdown hook by using the following method:
  1. public void addShutdownHook(Thread hook){}    
  2. Runtime r=Runtime.getRuntime();  
  3. r.addShutdownHook(new MyThread());  
Some important points about shutdown hooks are :
  • Shutdown hooks initialized but can only be started when JVM shutdown occurred.
  • Shutdown hooks are more reliable than the finalizer() because there are very fewer chances that shutdown hooks not run.
  • The shutdown hook can be stopped by calling the halt(int) method of Runtime class

20)When should we interrupt a thread?

We should interrupt a thread when we want to break out the sleep or wait state of a thread. We can interrupt a thread by calling the interrupt() throwing the InterruptedException.

21) What is the synchronization?

Synchronization is the capability to control the access of multiple threads to any shared resource. It is used:

  1. To prevent thread interference.
  2. To prevent consistency problem.
When the multiple threads try to do the same task, there is a possibility of an erroneous result, hence to remove this issue, Java uses the process of synchronization which allows only one thread to be executed at a time. Synchronization can be achieved in three ways:
  • by the synchronized method
  • by synchronized block
  • by static synchronization
Syntax for synchronized block
  1. synchronized(object reference expression)  
  2.     {  
  3.         //code block  
  4.     }  
  5.       

Java Multithreading Interview Questions

22) What is the purpose of the Synchronized block?

The Synchronized block can be used to perform synchronization on any specific resource of the method. Only one thread at a time can execute on a particular resource, and all other threads which attempt to enter the synchronized block are blocked.
  • Synchronized block is used to lock an object for any shared resource.
  • The scope of the synchronized block is limited to the block on which, it is applied. Its scope is smaller than a method.

23)Can Java object be locked down for exclusive use by a given thread?

Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

24) What is static synchronization?

If you make any static method as synchronized, the lock will be on the class not on the object. If we use the synchronized keyword before a method so it will lock the object (one thread can access an object at a time) but if we use static synchronized so it will lock a class (one thread can access a class at a time).

25)What is the difference between notify() and notifyAll()?

The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unlock all the threads in waiting state.

26)What is deadlock?

Deadlock is a situation in which every thread is waiting for a resource which is held by some other waiting thread. In this situation, Neither of the thread executes nor it gets the chance to be executed. Instead, there exists a universal waiting state among all the threads. Deadlock is a very complicated situation which can break our code at runtime.

27) How to detect a deadlock condition? How can it be avoided?

We can detect the deadlock condition by running the code on cmd and collecting the Thread Dump, and if any deadlock is present in the code, then a message will appear on cmd.
Ways to avoid the deadlock condition in Java:
  • Avoid Nested lock: Nested lock is the common reason for deadlock as deadlock occurs when we provide locks to various threads so we should give one lock to only one thread at some particular time.
  • Avoid unnecessary locks: we must avoid the locks which are not required.
  • Using thread join: Thread join helps to wait for a thread until another thread doesn't finish its execution so we can avoid deadlock by maximum use of join method.

28) What is Thread Scheduler in java?

In Java, when we create the threads, they are supervised with the help of a Thread Scheduler, which is the part of JVM. Thread scheduler is only responsible for deciding which thread should be executed. Thread scheduler uses two mechanisms for scheduling the threads: Preemptive and Time Slicing.
Java thread scheduler also works for deciding the following for a thread:
  • It selects the priority of the thread.
  • It determines the waiting time for a thread
  • It checks the Nature of thread

29) Does each thread have its stack in multithreaded programming?

Yes, in multithreaded programming every thread maintains its own or separate stack area in memory due to which every thread is independent of each other.

30) How is the safety of a thread achieved?

If a method or class object can be used by multiple threads at a time without any race condition, then the class is thread-safe. Thread safety is used to make a program safe to use in multithreaded programming. It can be achieved by the following ways:
  • Synchronization
  • Using Volatile keyword
  • Using a lock based mechanism
  • Use of atomic wrapper classes

31) What is race-condition?

A Race condition is a problem which occurs in the multithreaded programming when various threads execute simultaneously accessing a shared resource at the same time. The proper use of synchronization can avoid the Race condition.

Java Multithreading Interview Questions

32) What is the volatile keyword in java?

Volatile keyword is used in multithreaded programming to achieve the thread safety, as a change in one volatile variable is visible to all other threads so one variable can be used by one thread at a time.

33) What do you understand by thread pool?

  • Java Thread pool represents a group of worker threads, which are waiting for the task to be allocated.
  • Threads in the thread pool are supervised by the service provider which pulls one thread from the pool and assign a job to it.
  • After completion of the given task, thread again came to the thread pool.
  • The size of the thread pool depends on the total number of threads kept at reserve for execution.
The advantages of the thread pool are :
  • Using a thread pool, performance can be enhanced.
  • Using a thread pool, better system stability can occur.

Concurrency Interview Questions

34) What are the main components of the concurrency API?

Concurrency API can be developed using the class and interfaces of java.util.Concurrent package. There are the following classes and interfaces in java.util.Concurrent package.
  • Executor
  • FarkJoinPool
  • ExecutorService
  • ScheduledExecutorService
  • Future
  • TimeUnit(Enum)
  • CountDownLatch
  • CyclicBarrier
  • Semaphore
  • ThreadFactory
  • BlockingQueue
  • DelayQueue
  • Locks
  • Phaser

35) What is the Executor interface in Concurrency API in Java?

The Executor Interface provided by the package java.util.concurrent is the simple interface used to execute the new task. The execute() method of Executor interface is used to execute some given command. The syntax of the execute() method is given below.
void execute(Runnable command)
Consider the following example:
  1. import java.util.concurrent.Executor;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ThreadPoolExecutor;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. public class TestThread {  
  7.    public static void main(final String[] arguments) throws InterruptedException {  
  8.       Executor e = Executors.newCachedThreadPool();  
  9.       e.execute(new Thread());  
  10.       ThreadPoolExecutor pool = (ThreadPoolExecutor)e;  
  11.       pool.shutdown();  
  12.    }    
  13.   
  14.    static class Thread implements Runnable {  
  15.       public void run() {  
  16.          try {  
  17.             Long duration = (long) (Math.random() * 5);  
  18.             System.out.println("Running Thread!");  
  19.             TimeUnit.SECONDS.sleep(duration);  
  20.             System.out.println("Thread Completed");  
  21.          } catch (InterruptedException ex) {  
  22.             ex.printStackTrace();  
  23.          }  
  24.       }  
  25.    }  
  26. }     
Output
Running Thread!
Thread Completed

Java Multithreading Interview Questions

36) What is BlockingQueue?

The java.util.concurrent.BlockingQueue is the subinterface of Queue that supports the operations such as waiting for the space availability before inserting a new value or waiting for the queue to become non-empty before retrieving an element from it. Consider the following example.
  1.       
  2. import java.util.Random;  
  3. import java.util.concurrent.ArrayBlockingQueue;  
  4. import java.util.concurrent.BlockingQueue;  
  5.   
  6. public class TestThread {  
  7.   
  8.    public static void main(final String[] arguments) throws InterruptedException {  
  9.       BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);  
  10.   
  11.       Insert i = new Insert(queue);  
  12.       Retrieve r = new Retrieve(queue);  
  13.   
  14.       new Thread(i).start();  
  15.       new Thread(r).start();  
  16.   
  17.       Thread.sleep(2000);  
  18.    }    
  19.   
  20.   
  21.    static class Insert implements Runnable {  
  22.       private BlockingQueue<Integer> queue;  
  23.   
  24.       public Insert(BlockingQueue queue) {  
  25.          this.queue = queue;  
  26.       }  
  27.   
  28.       @Override  
  29.       public void run() {  
  30.          Random random = new Random();  
  31.   
  32.          try {  
  33.             int result = random.nextInt(200);  
  34.             Thread.sleep(1000);  
  35.             queue.put(result);  
  36.             System.out.println("Added: " + result);  
  37.               
  38.             result = random.nextInt(10);  
  39.             Thread.sleep(1000);  
  40.             queue.put(result);  
  41.             System.out.println("Added: " + result);  
  42.               
  43.             result = random.nextInt(50);  
  44.             Thread.sleep(1000);  
  45.             queue.put(result);  
  46.             System.out.println("Added: " + result);  
  47.          } catch (InterruptedException e) {  
  48.             e.printStackTrace();  
  49.          }  
  50.       }      
  51.    }  
  52.   
  53.    static class Retrieve implements Runnable {  
  54.       private BlockingQueue<Integer> queue;  
  55.   
  56.       public Retrieve(BlockingQueue queue) {  
  57.          this.queue = queue;  
  58.       }  
  59.         
  60.       @Override  
  61.       public void run() {  
  62.            
  63.          try {  
  64.             System.out.println("Removed: " + queue.take());  
  65.             System.out.println("Removed: " + queue.take());  
  66.             System.out.println("Removed: " + queue.take());  
  67.          } catch (InterruptedException e) {  
  68.             e.printStackTrace();  
  69.          }  
  70.       }  
  71.    }  
  72. }  
Output
Added: 96
Removed: 96
Added: 8
Removed: 8
Added: 5
Removed: 5

37) How to implement producer-consumer problem by using BlockingQueue?

The producer-consumer problem can be solved by using BlockingQueue in the following way.
  1.       
  2. import java.util.concurrent.BlockingQueue;  
  3. import java.util.concurrent.LinkedBlockingQueue;  
  4. import java.util.logging.Level;  
  5. import java.util.logging.Logger;  
  6. public class ProducerConsumerProblem {  
  7.     public static void main(String args[]){  
  8.      //Creating shared object  
  9.      BlockingQueue sharedQueue = new LinkedBlockingQueue();  
  10.   
  11.      //Creating Producer and Consumer Thread  
  12.      Thread prod = new Thread(new Producer(sharedQueue));  
  13.      Thread cons = new Thread(new Consumer(sharedQueue));  
  14.   
  15.      //Starting producer and Consumer thread  
  16.      prod.start();  
  17.      cons.start();  
  18.     }  
  19.    
  20. }  
  21.   
  22. //Producer Class in java  
  23. class Producer implements Runnable {  
  24.   
  25.     private final BlockingQueue sharedQueue;  
  26.   
  27.     public Producer(BlockingQueue sharedQueue) {  
  28.         this.sharedQueue = sharedQueue;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void run() {  
  33.         for(int i=0; i<10; i++){  
  34.             try {  
  35.                 System.out.println("Produced: " + i);  
  36.                 sharedQueue.put(i);  
  37.             } catch (InterruptedException ex) {  
  38.                 Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43. }  
  44.   
  45. //Consumer Class in Java  
  46. class Consumer implements Runnable{  
  47.   
  48.     private final BlockingQueue sharedQueue;  
  49.   
  50.     public Consumer (BlockingQueue sharedQueue) {  
  51.         this.sharedQueue = sharedQueue;  
  52.     }  
  53.     
  54.     @Override  
  55.     public void run() {  
  56.         while(true){  
  57.             try {  
  58.                 System.out.println("Consumed: "+ sharedQueue.take());  
  59.             } catch (InterruptedException ex) {  
  60.                 Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);  
  61.             }  
  62.         }  
  63.     }  
  64. }  
Output
Produced: 0
Produced: 1
Produced: 2
Produced: 3
Produced: 4
Produced: 5
Produced: 6
Produced: 7
Produced: 8
Produced: 9
Consumed: 0
Consumed: 1
Consumed: 2
Consumed: 3
Consumed: 4
Consumed: 5
Consumed: 6
Consumed: 7
Consumed: 8
Consumed: 9

38) What is the difference between Java Callable interface and Runnable interface?

The Callable interface and Runnable interface both are used by the classes which wanted to execute with multiple threads. However, there are two main differences between the both :
  • A Callable <V> interface can return a result, whereas the Runnable interface cannot return any result.
  • A Callable <V> interface can throw a checked exception, whereas the Runnable interface cannot throw checked exception.
  • A Callable <V> interface cannot be used before the Java 5 whereas the Runnable interface can be used.

39) What is the Atomic action in Concurrency in Java?

  • The Atomic action is the operation which can be performed in a single unit of a task without any interference of the other operations.
  • The Atomic action cannot be stopped in between the task. Once started it fill stop after the completion of the task only.
  • An increment operation such as a++ does not allow an atomic action.
  • All reads and writes operation for the primitive variable (except long and double) are the atomic operation.
  • All reads and writes operation for the volatile variable (including long and double) are the atomic operation.
  • The Atomic methods are available in java.util.Concurrent package.

40) What is lock interface in Concurrency API in Java?

The java.util.concurrent.locks.Lock interface is used as the synchronization mechanism. It works similar to the synchronized block. There are a few differences between the lock and synchronized block that are given below.
  • Lock interface provides the guarantee of sequence in which the waiting thread will be given the access, whereas the synchronized block doesn't guarantee it.
  • Lock interface provides the option of timeout if the lock is not granted whereas the synchronized block doesn't provide that.
  • The methods of Lock interface, i.e., Lock() and Unlock() can be called in different methods whereas single synchronized block must be fully contained in a single method.

41) Explain the ExecutorService Interface.

The ExecutorService Interface is the subinterface of Executor interface and adds the features to manage the lifecycle. Consider the following example.
  1.       
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. public class TestThread {  
  7.    public static void main(final String[] arguments) throws InterruptedException {  
  8.       ExecutorService e = Executors.newSingleThreadExecutor();  
  9.   
  10.       try {  
  11.          e.submit(new Thread());  
  12.          System.out.println("Shutdown executor");  
  13.          e.shutdown();  
  14.          e.awaitTermination(5, TimeUnit.SECONDS);  
  15.       } catch (InterruptedException ex) {  
  16.          System.err.println("tasks interrupted");  
  17.       } finally {  
  18.   
  19.          if (!e.isTerminated()) {  
  20.             System.err.println("cancel non-finished tasks");  
  21.          }  
  22.          e.shutdownNow();  
  23.          System.out.println("shutdown finished");  
  24.       }  
  25.    }  
  26.   
  27.    static class Task implements Runnable {  
  28.         
  29.       public void run() {  
  30.            
  31.          try {  
  32.             Long duration = (long) (Math.random() * 20);  
  33.             System.out.println("Running Task!");  
  34.             TimeUnit.SECONDS.sleep(duration);  
  35.          } catch (InterruptedException ex) {  
  36.             ex.printStackTrace();  
  37.          }  
  38.       }  
  39.    }         
  40. }  
Output
Shutdown executor
shutdown finished

42) What is the difference between Synchronous programming and Asynchronous programming regarding a thread?

Synchronous programming: In Synchronous programming model, a thread is assigned to complete a task and hence thread started working on it, and it is only available for other tasks once it will end the assigned task.
Asynchronous Programming: In Asynchronous programming, one job can be completed by multiple threads and hence it provides maximum usability of the various threads.

43) What do you understand by Callable and Future in Java?

Java Callable interface: In Java5 callable interface was provided by the package java.util.concurrent. It is similar to the Runnable interface but it can return a result, and it can throw an Exception. It also provides a run() method for execution of a thread. Java Callable can return any object as it uses Generic.
Syntax:
public interface Callable<V>
Java Future interface: Java Future interface gives the result of a concurrent process. The Callable interface returns the object of java.util.concurrent.Future.
Java Future provides the following methods for implementation.
  • cancel(boolean mayInterruptIfRunning): It is used to cancel the execution of the assigned task.
  • get(): It waits for the time if execution not completed and then retrieved the result.
  • isCancelled(): It returns the Boolean value as it returns true if the task was canceled before the completion.
  • isDone(): It returns true if the job is completed successfully else returns false.

44. What is the difference between ScheduledExecutorService and ExecutorService interface?

ExecutorServcie and ScheduledExecutorService both are the interfaces of java.util.Concurrent package but scheduledExecutorService provides some additional methods to execute the Runnable and Callable tasks with the delay or every fixed time period.

45) Define FutureTask class in Java?

Java FutureTask class provides a base implementation of the Future interface. The result can only be obtained if the execution of one task is completed, and if the computation is not achieved then get method will be blocked. If the execution is completed, then it cannot be re-started and can't be canceled.

Java String Interview Questions and Answers

1) What is String in Java? Is String is data type? String in Java is not a primitive data type like int, long or double. The string is a ...