Vous êtes sur la page 1sur 24

Stages of Life Cycle

There are various stages of life cycle of thread


New
Runnable
Running
Waiting
Dead
Contd…
• New: In this phase, the thread is created using class "Thread class".
It remains in this state till the program starts the thread. It is also
known as born thread.
• Runnable: In this phase, the instance of the thread is invoked with a
start method. The thread control is given to scheduler to finish the
execution. It depends on the scheduler, whether to run the thread.
• Running: When the thread starts executing, then the state is
changed to "running" state. The scheduler selects one thread from
the thread pool, and it starts executing in the application.
• Waiting: This is the state when a thread has to wait. As there
multiple threads are running in the application, there is a need for
synchronization between threads. Hence, one thread has to wait, till
the other thread gets executed. Therefore, this state is referred as
waiting state.
• Dead: This is the state when the thread is terminated. The thread is
in running state and as soon as it completed processing it is in "dead
state".
Methods for Threads
• start()- This method starts the execution of the
thread and JVM calls the run() method on the
thread.
• sleep(int milliseconds)- This method makes
the thread sleep hence the thread's execution
will pause for milliseconds provided and after
that, again the thread starts executing. This
help in synchronization of the threads.
Methods for Threads
• getName()- It returns the name of the thread.
• setPriority(int newpriority)- It changes the
priority of the thread.
• yield ()- It causes current thread on halt and
other threads to execute.
• Multitasking: Ability to execute more than one
task at the same time is known as multitasking.
• Multithreading: We already discussed about it. It
is a process of executing multiple threads
simultaneously. Multithreading is also known as
Thread-based Multitasking.
• Multiprocessing: It is same as multitasking,
however in multiprocessing more than one CPUs
are involved. On the other hand one CPU is
involved in multitasking.
• Parallel Processing: It refers to the utilization of
multiple CPUs in a single computer system.
Creating a thread in Java
There are two ways to create a thread in Java:
1) By extending Thread class.
2) By implementing Runnable interface.
Thread creation by extending
Thread class
Thread creation by extending
Thread class
Thread By Implementing Runnable Interface
• A Thread can be created by extending Thread class also. But
Java allows only one class to extend, it wont allow multiple
inheritance. So it is always better to create a thread by
implementing Runnable interface. Java allows you to
impliment multiple interfaces at a time.
• By implementing Runnable interface, you need to provide
implementation for run() method.
• To run this implementation class, create a Thread object, pass
Runnable implementation class object to its constructor. Call
start() method on thread class to start executing run()
method.
• Implementing Runnable interface does not create a Thread
object, it only defines an entry point for threads in your
object. It allows you to pass the object to the
Thread(Runnable implementation) constructor.
class X implements Runnable{
public void run() {
for(int i=1;i<=10;i++)
{
System.out.println(“Threadx” +i);
}
System.out.println("End of ThreadX”);
}
}
public class RunnableTest {
public static void main(String a[]){
X runnable = new X();
Thread threadX = new Thread(runnable);
threadX.start();
System.out.println(“End of main Thread”);
}
}
Output
End of main Thread
ThreadX: 1
...
ThreadX: 10

End of ThreadX...
Java Synchronization
• Multithreading programs may often come to a situation
where multiple threads try to access the same resources and
finally produce erroneous and unforeseen results.
• So it needs to be made sure by some synchronization
method that only one thread can access the resource at a
given point of time.
• Java provides a way of creating threads and synchronizing
their task by using synchronized blocks. Synchronized
blocks in Java are marked with the synchronized keyword.
A synchronized block in Java is synchronized on some
object. All synchronized blocks synchronized on the same
object can only have one thread executing inside them at a
time. All other threads attempting to enter the synchronized
block are blocked until the thread inside the synchronized
block exits the block.

Vous aimerez peut-être aussi