This ppt gives a general idea about the multithreading concepts in the java programming language. hope you find it useful
P.S :
sorry there is a correction in one of the slides
where i have entered implements thread
it is wrong it is actually implements Runnable
thank you!
Micro-Scholarship, What it is, How can it help me.pdf
ย
Threads concept in java
1. Multithreading
โ A technique by which a single set of code can
be used by several processors at different
stages of execution.
2. Process and Threads
โ A process is a thread in execution.
โ A process may be divided ninto number of
independent units known as threads.
โ A thread is a dispatchable unit of work.
โ Threads are light-weight processes within a
process .
โ A process is a collection of one or more
threads and associated system resources
3. Difference between Process and
Threads
โ Process can be
divided into multiple
threads
โ Each process has its
own memory space
โ It is difficult to create
a process
โ Threads cannot be
sub divided.
โ Threads of the same
process share a
common memory
space
โ It is easy to create a
thread.
4. Multitasking
โ Multitasking is a method where multiple tasks
are performed during the same period of time
โ They are executed concurrently instead of
sequentially
โ The tasks share common processing
resources, such as a CPU and main memory.
5. Difference between multitasking and
multithreading
โ An ability to run
several programs
simultaneously
potentially by using
several processors or
by time sharing the
resources available.
โ An ability to run
serveral processes of
a single program
simultaneously
potentially using
several processors or
by time sharing the
resources available.
7. Contd'
โ
NEW- A thread that is just instantiated is in new state. When a start()
method is invoked, the thread moves to the ready state from which it is
automatically moved to runnable state by the thread scheduler.
โ RUNNABLE (ready running)-A thread executing in the JVM is in running
state.
โ
BLOCKED - A thread that is blocked waiting for a monitor lock is in this
state. This can also occur when a thread performs an I/O operation and
moves to next (runnable) state.
โ WAITING - A thread that is waiting indefinitely for another thread to
perform a particular action is in this state.
โ
TIMED_WAITING - A thread that is waiting for another thread to perform
an action for up to a specified waiting time is in this state.
โ TERMINATED - A thread that has exited is in this state.
8. Thread Creation
Threads can be created using two ways
โ By implementing the Runnable class
โ By extending the Thread class
โ start() method is used to start the thread
โ run() method is executed after calling the start()
โ run() can contain the code you wish to perform
using thread.
9. Some of methods used in Threads
โ start()- to begin the execution of thread
โ setName()- to set the name for a thread
โ getName()- to get the name of a thread
โ Sleep() - to make the thread wait for specified
amount of time.
โ setPriority()- to set the priority of the threads
โ getPriority()- to get the priority of threads
10. Thread Creation Example
public class Threadlearning extends Thread
{
public void run()
{
System.out.println("i am thread ");
}
public static void main(String[] args)
{
Threadlearning t = new Threadlearning();
t.start();
}
}
public class Thread2way implements
Runnable
{
public void run()
{
System.out.println("i am in thread");
}
public static void main(String args[])
{
(new Thread(new Thread2way())).start();
}
}