Prev Next

Java / Java Multithreading part II

Could not find what you were looking for? send us the question and we would be happy to answer your question.

What are Executors in Java?

Executors are objects that encapsulate thread management and separate thread creation from the rest of the application.

What are the Executor interfaces?

The java.util.concurrent package defines 3 executor interfaces:

  • Executor, a simple interface that supports launching new tasks.
  • ExecutorService, a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
  • ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks.
Is main thread a daemon thread?

No, not even the child threads created by the main thread unless explicitly set as the daemon by setDaemon(true) method.

Explain Amdahl's law.

Amdahl's law is a formula which gives the theoretical speedup in latency of the execution of a task at the fixed workload that can be expected of a system whose resources are improved.

Can the Thread run method be overloaded?

Yes. You may overload the run method. However, The overloaded run method will be ignored by the Thread start method and it always starts the run method with no args.

public class OverloadedRunTest extends Thread {

	public static void main(String[] args) {
		OverloadedRunTest ft1 = new OverloadedRunTest();
		ft1.start(); // Separate call stack"Hello"); // same call stack

	public void run() {

		System.out.println("Thread is running");
		run("With parameter");

	public void run(String s) {
		System.out.println("Overloaded Thread is running " + s + " " + Thread.currentThread().getName());
Can you override Thread start method?

Yes, however doing so invoking start method will execute start on the same thread and it will neither create new thread nor call run method.

public class ThreadOverrideStart extends Thread{
	public void run() {
		System.out.println("Running in " + Thread.currentThread().getName());
	public void start() {
		System.out.println(" Hello from start method");
	public static void main(String[] args) {
		ThreadOverrideStart th = new ThreadOverrideStart();


In the above program, both the run and start method are overridden. Some may expect that the start method will invoke the run method but it won't. Invoking start method works like any other method and will not invoke run method at all.


 Hello from start method
Why most of the thread functionalities are specified in Object Class?

Most of the thread functionalities are specified in object class to facilitate interthread communication.

What is Latency?

Latency is the measure of time taken to respond to a something (Total Response time). It is considered the mix of active time and dead time.

Active time is the measure of when a thread is making forward progress while the dead time is when the thread is stalled.

Low latency is usually something not noticed by human and seamless.

What is JVM Safe-pointing?

When JVM needs to perform maintenance, it gets exclusive access pausing all the application threads and also save threads and its state. which is called safe-pointing.

Safe-pointing is performed when doing garbage collection, code cache maintenance.

Which is faster?
(a) public void incrementI() {

synchronized (this) {
 i = i + 1;


(b) public synchronized void incrementI() {

 i = i + 1;


Option a is faster. Because option b, which is synchronizing on the method call has to run many instructions than option a which deals only with the critical section.

What is JVM tiered compilation?

Tiered compilation, introduced in Java SE 7, brings client startup speeds to the server VM by enabling client compiler usage instead of interpreter. Once the code becomes hot and profile data is collected, it is recompiled by the Server compiler. Tiered compilation is enabled by default on server VM.

Types of thread in Java.

There are 2 types.

  • User thread,
  • Daemon thread.

User threads are threads created by the application or user. They are high priority threads. JVM (Java Virtual Machine) will wait (not exit) until all user threads finish their execution. These threads are foreground threads.

Daemon threads are threads mostly created by the JVM. These threads always run in background to perform some background tasks like garbage collection and house-keeping tasks. These threads are less priority threads. JVM will exit as soon as all user threads finish their execution. JVM doesn’t wait for daemon threads to finish their task.

Is JVM a process or thread?

JVM is a process.

Can you clone a Thread?

No. We will encounter CloneNotSupportedException.

What is effectively immutable in Java?

Instance of classes that are not extensible and has final fields are considered immutable.

Effectively immutable are object that may have non final fields (private) however there may not any means to change it’s value.

What is busy spinning in Java?

Busy spinning is a waiting strategy in which a thread just wait in a loop, without releasing the CPU just by going to sleep. By not releasing the CPU or suspending the thread, your thread retains all the cached data and instruction, which may be lost if the thread was suspended and resumed back in a different core of CPU.

This is popular in high-frequency low latency programming domain, where programmers are trying for extremely low latency in the range of micro to milliseconds.

private volatile boolean isDone = false;

public void waitUntilDone() {
while (!isDone) {

Explain cache coherence protocol.

Cache coherence protocol is the protocol CPUs use to transfer cache lines between main memory and other CPUs.

Can I use "synchronized" keyword on a String object?

String is not a very good candidate to be used with synchronized keyword. String objects are stored in a string pool and we don't want to lock a string that might be getting used by another piece of code.

final keyword

Comments & Discussions