1.) What is thread?
Thread can refer to two different things:-
- An object of class java.lang.Thread.
Like any other object in Java, it has variables and methods, and lives and dies on the heap.
- A thread of execution
thread of execution is an individual process (a “lightweight” process) that has its own call stack. In Java,
there is one thread per call stack—or, to think of it in reverse, one call stack per
thread. Even if you don’t create any new threads in your program, threads are back there running.
The main() method runs in one thread, called the main thread. If we look at the main call stack
we see that main() is the first method on the stack— the method at the bottom. But as soon as you create a new thread, a new stack materializes and methods called from that thread run in a call stack that’s separate
from the main() call stack. That second new call stack is said to run concurrently with the main stack.
2.)How do you implement Thread in Java?
You can define and instantiate a thread in one of two ways:
1 Extend the java.lang.Thread class.
2 Implement the Runnable interface.
1.Extending java.lang.Thread
The simplest way to define code to run in a separate thread is to
Extend the java.lang.Thread class.
Override the run() method.
It looks like this:
class MyThread extends Thread {
public void run() {
System.out.println(“Important job running in MyThread”);}
if you extend Thread, you can’t extend anything else
2.Implementing java.lang.Runnable
Implementing the Runnable interface gives a way to extend any class we like,
but still define behavior that will be run by a separate thread. It looks like this:
class MyRunnable implements Runnable {
public void run() {
System.out.println(“Important job running in MyRunnable”);
}
}
3.) When to use Runnable vs Thread in Java?
if you extend Thread, you can’t extend anything else,in that case we can use runnable .
4.)What are the different states of a thread’s lifecycle?
New – When a thread is instantiated it is in New state until the start() method is called on the thread instance. In this state the thread is not considered to be alive.
Runnable – The thread enters into this state after the start method is called in the thread instance. The thread may enter into the Runnable state from Running state. In this state the thread is considered to be alive.
Running – When the thread scheduler picks up the thread from the Runnable thread’s pool, the thread starts running and the thread is said to be in Running state.
Waiting/Blocked/Sleeping – In these states the thread is said to be alive but not runnable. The thread switches to this state because of reasons like wait method called or sleep method has been called on the running thread or thread might be waiting for some i/o resource so blocked.
Dead – When the thread finishes its execution i.e. the run() method execution completes, it is said to be in dead state. A dead state can not be started again. If a start() method is invoked on a dead thread a run time exception will occur.
5) Difference between start() and run() method of Thread class?
So what happens after we call start()?
The good stuff:
A new thread of execution starts (with a new call stack).
The thread moves from the new state to the runnable state.
When the thread gets a chance to execute, its target run() method will run.
There’s nothing special about the run() method as far as Java is
concerned. Like main(), it just happens to be the name (and signature) of the method
that the new thread knows to invoke. So if you see code that calls the run() method on
a Runnable (or even on a Thread instance), that’s perfectly legal. But it doesn’t mean the
run() method will run in a separate thread! Calling a run() method directly just means
you’re invoking a method from whatever thread is currently executing, and the run()
method goes onto the current call stack rather than at the beginning of a new call stack.
The following code does not start a new thread of execution:
Thread t = new Thread();
t.run(); // Legal, but does not start a new thread
6) Is it possible to start a thread twice?
If we have a reference to a Thread, and you call start(), it’s started. If you call
start() a second time, it will cause an exception (an IllegalThreadStateException,
which is a kind of RuntimeException, but you don’t need to worry about the exact
type). This happens whether or not the run() method has completed from the first
start() call. Only a new thread can be started, and then only once. A runnable
thread or a dead thread cannot be restarted.
7) Can we call the run() method instead of start()?
Calling a run() method directly just means
you’re invoking a method from whatever thread is currently executing, and the run()
method goes onto the current call stack rather than at the beginning of a new call stack.
The following code does not start a new thread of execution:
Thread t = new Thread();
t.run(); // Legal, but does not start a new thread
8) What is the difference between wait() and sleep()?
1 wait() is a method of Object class. | sleep()is a static method of class Thread. | |
2 sleep() allows the thread to go to sleep state for x milliseconds . When a thread goes into sleep state it doesn’t release the lock.Thread become active after the sleeping time is over | wait() allows thread to release the lock and goes to suspended state.The thread is only active when a notify() or notifAll() method is called for the same object | |
9) What is the difference between yield() and sleep()?
yield will cause the currently running thread to move back to runnable
so that a thread of the same priority can have a chance.
sleep() allows the current thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t releases the lock.
10)What is the use of join method?
The non-static join() method of class Thread lets one thread “join onto the end”
of another thread. If you have a thread B that can’t do its work until another thread
A has completed its work, then you want thread B to “join” thread A. This means that
thread B will not become runnable until A has finished (and entered the dead state).
Thread t = new Thread();
t.start();
t.join();
The preceding code takes the currently running thread (if this were in the
main() method, then that would be the main thread) and joins it to the end of the
thread referenced by t. This blocks the current thread from becoming runnable
until after the thread referenced by t is no longer alive. In other words, the
code t.join() means “Join me (the current thread) to the end of t, so that t
must finish before I (the current thread) can run again.”