0% found this document useful (0 votes)
9 views12 pages

CH 3 Multithreading

The document provides an overview of multithreading, explaining the difference between process-based and thread-based multitasking, with threads being lightweight and low-cost. It details the lifecycle of a thread, methods for creating threads in Java, and various thread management functions. Additionally, it discusses synchronization in Java, its advantages and disadvantages, including responsiveness and potential deadlocks.

Uploaded by

amareguadie8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

CH 3 Multithreading

The document provides an overview of multithreading, explaining the difference between process-based and thread-based multitasking, with threads being lightweight and low-cost. It details the lifecycle of a thread, methods for creating threads in Java, and various thread management functions. Additionally, it discusses synchronization in Java, its advantages and disadvantages, including responsiveness and potential deadlocks.

Uploaded by

amareguadie8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multithreading

1
 Process is an execution environment provided by the operating system that has it’s own set of
private resources

 Thread live within a process and share their resources with other threads of the process

Multitasking

 Is a process of executing multiple tasks simultaneously (to utilize the cpu)

 Can be achieved by two ways

1. Process – based multitasking (Multi processing)


 A process is given a fixed time to run

 When time is up operating system suspends the process and switches context to another process
2
 Process are heavy weight
2. Thread – based multitasking (Multithreading)
 Run multiple threads at the same time

 Thread is light weight

 Low cost

Life cycle of a Thread

Runnable
New Blocked

Terminated Running
3
 Java thread are created by

1. Extending the Thread class


 Override the run() method

 Instantiate the class

 Call start ()

2. Implementing the runnable Interface


 Create a class that implement Runnable

 Implement the run() method

 Instantiate the class you created and Thread class


4
 Call start()
Class Worker extends Thread {
Public void run() {
[Link](“Worker Thread”);
}
}
Public class First {
Public static void main(String args[]){
Worker r=new Worker();
[Link]();

}
}

5
Class Demo implements Runnable{
Public void run() {
[Link](“running state”);
}
}
Public class First {
Public static void main(String args[]){
Demo obj= new Demo();
Thread t= new Thread(obj);
[Link]();
}
}

6
 start(): - new Thread is created
 run(): - entry point for the thread
 String getName():- to get the name of the current thread
 setName(String Name): for setting name
 sleep(long milliseconds): suspends a thread for the specified period.
 boolean isAlive():- determines if a thread is running
 join():- wait for thread to die gets called when a thread wants to wait for another thread to terminate.
 currentThread():- It returns the instance of the currently executing thread
 State getState():- returns the state of the thread
 getPriority():- returns the priority of the thread
 setPriority(int priority):- change the priority of the thread .
 interrupts():- interrupts the thread
 boolean is Interrupted():- tests if the thread has been interrupted.
 suspend():- suspend the thread
 resume(): resume the thread 7
 stop(): halt the thread.
Public class SleepExample extends Thread {
Public void run() {
for (int i=1; i<3; i++) { [Link](i);
try {[Link](5000);} catch(Interrupted Exception e)
{ [Link](e); }
}
}
}
Public class First {
Public static void main(String args[]){
SleepExample t1=new SleepExample(); SleepExample t2=new SleepExample();
[Link](); [Link]();
}
8
}
Public class J extends Thread {
Public void run() {
for (int i=1; i<3; i++) { [Link](i);
try { [Link](5000); } catch(Interrupted Exception e)
{ [Link](e); }
}
}
}
Public class First {
Public static void main(String args[]){
J t1=new J(); J t2=new J(); J t3=new J();
try { [Link](); } [Link](); [Link]();
}
9
}
Public class P extends Thread {
Public void run() {
[Link](“Name”+[Link]().getName());
[Link](“Priority”+[Link]().getPriority());
}
}
Public class First {
Public static void main(String args[]){
P m1=new P(); P m2=new P();
[Link]((Thread.MIN_PRIORITY));
[Link]((Thread.MAX_PRIORITY));
[Link](); [Link]();
}
10
}
Synchronization in java
 Is the capability to control the access of multiple threads to any shared resource

 To allow only one thread to access the shared resources

 To prevent thread interference

 To prevent consistency problem

11
Advantages

 Responsiveness

 Resource sharing

 Economy

 Utilization of multiprocessor architectures

Disadvantages

 More complicated code

 Deadlocks

12

You might also like