threads concept in java

10
Multithreading A technique by which a single set of code can be used by several processors at different stages of execution.

Upload: muthukumaran-subramanian

Post on 24-May-2015

77 views

Category:

Education


0 download

DESCRIPTION

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!

TRANSCRIPT

Page 1: Threads concept in java

Multithreading

● A technique by which a single set of code can be used by several processors at different stages of execution.

Page 2: Threads concept in java

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

Page 3: Threads concept in java

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.

Page 4: Threads concept in java

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.

Page 5: Threads concept in java

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.

Page 6: Threads concept in java

Life cycle of Thread

Page 7: Threads concept in java

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.

Page 8: Threads concept in java

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.

Page 9: Threads concept in java

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

Page 10: Threads concept in java

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();

}

}