Java / Java Multithreading part II
(a) public void incrementI() { synchronized (this) { i = i + 1; } }
or
(b) public synchronized void incrementI() { i = i + 1; }
Executors are objects that encapsulate thread management and separate thread creation from the rest of the application.
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.
No, not even the child threads created by the main thread unless explicitly set as the daemon by setDaemon(true)
method.
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.
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 ft1.run("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()); } }
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()); } @Override public void start() { System.out.println(" Hello from start method"); } public static void main(String[] args) { ThreadOverrideStart th = new ThreadOverrideStart(); th.start(); } }
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.
Output:
Hello from start method
Most of the thread functionalities are specified in object class to facilitate interthread communication.
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.
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.
(a) public void incrementI() { synchronized (this) { i = i + 1; } }
or
(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.
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.
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 doesnt wait for daemon threads to finish their task.
JVM is a process.
No. We will encounter CloneNotSupportedException.
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.
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) { Thread.sleep(1000); } }
Cache coherence protocol is the protocol CPUs use to transfer cache lines between main memory and other CPUs.
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.