1⟩ Name the methods available in the Runnable Interface?
run()
“Java multi-threading frequently Asked Questions by expert members with experience in Java Multi-Threading. These interview questions and answers on Java multi-threading will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. So get preparation for the Java multi-threading job interview”
run()
Timeslicing is the method of allocating CPU time to individual threads in a priority schedule.
A process can contain multiple threads.
A process has its own memory address space whereas a thread doesn't.
Threads share the heap belonging to their parent process.
One process cannot corrupt another process
A thread can write the memory used by another thread.
In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.
In 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.
Briefly explain daemon thread.
Daemon thread is a low priority thread which runs in the background performs garbage collection operation for the java runtime system.
Two types of multitasking are Process-based and Thread-based
setPriority()
main thread
long
10,
1,
5
A user thread is created by the application (user) while the daemon thread is created by the JVM to provide services to user threads.
If user threads are exists then only daemon threads exists.
You can make a user thread as daemon thread by using setDaemon(true) method.
In an application when only daemon threads are running (i.e. no user thread is running) then JVM will shut down the application and subsequently stops all daemon threads.
To keep the application running, at least one user thread should be running.
Following are the different states of threads:
1. New – A thread is called in New state until the start() method is called for the object of that thread. The thread is not alive in this state.
2. Runnable – The thread is called in Runnable state after the start() method is called for the object of that thread. The thread may enter into the Runnable state from Running state. The thread is called alive in this state.
3. Running – The thread is called in Running state when the thread scheduler take it from the Runnable thread’s pool.
4. Waiting/Blocked/Sleeping – In these states the thread is alive but it’s not in Runnable state. A running thread is called in these states after wait() or sleep() method is called that thread or when the thread is blocked while waiting for I/O resources.
5. Dead – After the execution of run() method is complete then the thread is called in dead state. Once a thread is dead then it cannot be start again.
Synchronisation
wait(),
notify() &
notifyall()
Thread(Runnable threadobject,String threadName)
{
}
isAlive(),
join(),
resume(),
suspend(),
stop(),
start(),
sleep(),
destroy()
Process has its own memory address while the thread share the address of the process by which it is created.
Thread can access the data segment of its process directly while processes can access their own copy of the data segment of its parent process.
Threads have almost no overheads while processes may have overheads.
We can create a new thread easily but to create new process we have to duplicate the parent process.
If any changes in main thread are occurred it can affect the behavior of the other threads of the same process. While in case of process if any changes is occurred in parent process it won’t affect the behavior of parent process.
The direct communication is possible between threads of same process while in case of same sibling processes only interprocess communication is possible.
If we instantiate a thread it is called in new state until the Start() method is called.
If we don't call a start() method for that thread instance, the thread is not called alive.
If we invoke run method without calling the start method for a thread instance, the code in run() method wil not be executed by a new thread but it will be executed by the existing thread only.
When more than one threads access the same variable then each thread will maintain its own copy of that variable in the form of local cache.
When a thread is changing the value of variable, it is actually changing the local cache not the main variable memory.
Every thread which is using the same variable doesn’t know anything about the values changed by another thread.
To overcome this problem, if you declare a variable as volatile, then whenever a thread is changing the value of a volatile, it is actually changing to the main memory. So, all other threads which are accessing the same variable can get the changed value.
low-priority
Following are the advantage of using threads in Java.
Threads support concurrent operations. For example, Server can handle multiple requests coming from different clients by managing separate thread for each request.
Threads often result in simpler programs. Updating of separate views can be managed by separate Thread to give continuous updates.
Threads provide a high degree of control on application.
Threading gives the concurrency in our application by simplified coding.
Using threading, a computer with more than one CPU can execute multiple threads on different functional units without using time sharing.
The cost of communication between threads is relatively low.