Threads are essentially sub
processes. Informally, a thread
of execution is the smallest sequence
of programmed instructions that can be managed independently by an operating
system scheduler. Thread behaves
like tasks that belong to a program and that can run
"simultaneously". The
implementation of threads and processes differs from one operating system to
another, but in most cases, a thread is contained inside a process.
Defining, instantiating and starting a Thread
–
There are
two way to define a thread one is my implementing a Runnable Interface and
other is extending Thread class.
1. Implementing Runnable interface –
1.1
Implement
java.lang.Runnable interface.
1.2
Implements
the run () method.
1.3
Runnable
is actually an interface, with the single run () method that we must provide.
1.4
The
simple example is as below –
public class ThreadRunnable implements Runnable{
@Override
public void run() {
System.out.println("Thread
from Runnable interface.");
}
}
1.5
The
following code would then create a thread and start it running:
ThreadRunnable
threadRunnable = new ThreadRunnable ();
new
Thread(threadRunnable).start();
2. Extending Thread class –
2.1
Extends
java.lang.Thread class.
2.2
Override
the run () method.
2.3
The
simple example is as below –
public class ThreadFromThread extends Thread{
@Override
public void run() {
System.out.println("Thread
from Thread class.");
}
}
2.4
The
following code would then create a thread and start it running-
ThreadFromThread
threadFromThread = new ThreadFromThread();
threadFromThread.start();
Every thread has a name for identification purposes. More
than one thread may have the same name. If a name is not specified when a
thread is created, a new name is generated for it. Some points those should be
noted here are –
1. A new
thread of execution starts (with a new call stack).
2. The thread moves from the new state to the
runnable state.
3. When the thread gets a chance to execute, its
target run () method will run.
4. Be sure here that we can start Thread not
Runnable.
5. We can call run () method on java.lang.Thread
class, but the meaning of this method is that it will use same call stack,
instead of creating a new call stack. So the code is valid but the effect is
nothing. The meaning of this it will not create a new thread.
Thread life cycle –
1.
Newborn state
2.
Runnable state
3.
Running state
4.
Blocked state
5.
Dead state
Thread
should be in any one state of above and it can be move from one state to
another by different methods and ways.
1.
Newborn state
- When we create a thread it will be in Newborn State. The thread is just
created still it is not running. We can move it to running mode by invoking the
start () method and it can be killed by using stop () method.
2.
Runnable State -
It means that thread is now ready for running and its waiting to give control. We
can move control to another thread by yield () method.
3.
Running State -
It means thread is in its execution mode because the control of CPU is given to
that particular thread.
4.
Blocked State -
A thread is called in Blocked State when it is not allowed to entering in
Runnable State or Running State. It happens when thread is in waiting mode,
suspended or in sleeping mode.
5.
Dead State -
When a thread is completed executing its run () method the life cycle of that
particular thread is end. We can kill thread by invoking stop () method for
that particular threads and sends it to be in Dead State.
No comments:
Post a Comment