Java / Java Multithreading part II
1. What are Executors in Java?
Executors are objects that encapsulate thread management and separate thread creation from the rest of the application.
2. 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. ScheduledExec...
3. 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.
4. 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.
5. 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 OverloadedRu...
6. 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{ @Override public void run () { System.out.println( "Running in " + Thread.currentThread().getName()); } @O...
7. 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 .
8. 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 n...
9. 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.
10. 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...
11. 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 threa...
12. Is JVM a process or thread?
JVM is a process.
13. Can you clone a Thread?
No. We will encounter CloneNotSupportedException.
14. 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 its value.
15. 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 i...
16. Explain cache coherence protocol.
Cache coherence protocol is the protocol CPUs use to transfer cache lines between main memory and other CPUs.
17. 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.