Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Chapter 3 - Multithreading in Java

The document discusses Java multithreading. It begins by asking brainstorming questions about processes, threads, multitasking, and multithreading. It then defines multitasking and describes how it can be achieved through process-based or thread-based approaches. It discusses what threads are, compares processes and threads, defines multithreading, and outlines the advantages of multithreading. It also covers the life cycle of a thread and the two main ways to create threads in Java: by extending the Thread class or implementing the Runnable interface.

Uploaded by

Ayano Boresa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter 3 - Multithreading in Java

The document discusses Java multithreading. It begins by asking brainstorming questions about processes, threads, multitasking, and multithreading. It then defines multitasking and describes how it can be achieved through process-based or thread-based approaches. It discusses what threads are, compares processes and threads, defines multithreading, and outlines the advantages of multithreading. It also covers the life cycle of a thread and the two main ways to create threads in Java: by extending the Thread class or implementing the Runnable interface.

Uploaded by

Ayano Boresa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Advance java Programming

# CoSc3052

Java
Multithreading

Mekonen M.
Computer Science Department
Hawassa University Daye campus, Daye
Chapter Three
Java Multithreading
Brainstorming question
1. What do you know about process and thread? What are some real-world examples of tasks
that would benefit from being divided into separate processes or threads?
2. What is the multitasking and how does it achieved?
3. What is multithread and how does it implement in java?
4. Explain the advantages of using multithreading in Java applications.
5. What are the key scheduling algorithms employed by thread schedulers, and what criteria
do they consider when making scheduling decisions?

Mekonen M. #CoSc3052  Multithreading 3


Multitasking
 Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to
utilize the CPU.
 Multitasking can be achieved in two ways:
1. Process-based Multitasking (Multiprocessing)
2. Thread-based Multitasking (Multithreading)
1. Process-based Multitasking (Multiprocessing)
 Each process has an address in memory. In other words, each process allocates a separate memory area.
 A process is heavyweight.
 Cost of communication between the process is high.
 Switching from one process to another requires some time for saving and loading registers, memory maps,
updating lists, etc.
2. Thread-based Multitasking (Multithreading)
 Threads share the same address space.
 A thread is lightweight.
 Cost of communication between the thread is low.

Mekonen M. #CoSc3052  Multithreading 4


What is the Thread?
 A thread is a lightweight sub-process, the smallest unit of processing.
 It is a separate path of execution.
 Threads are independent. If there occurs exception in one thread, it doesn't
affect other threads.
 It uses a shared memory area.
 Thread: single sequential flow of control within a program
 Single-threaded program can handle one task at any time.
 Multitasking allows single processor to run several concurrent threads.
 Most modern operating systems support multitasking.
 With Java, you can launch multiple threads from a program concurrently.
 These threads can be executed simultaneously in multiprocessor systems, as
shown in Figure below.
Mekonen M. #CoSc3052  Multithreading 5
Multiple threads
are running on
multiple CPUs

Multiple threads
sharing a single
CPU

Mekonen M. #CoSc3052  Multithreading 6


Process vs Thread
Process thread
Process is a program in execution. A thread is a subset (part) of the process.
A process consists of multiple threads. A thread is a smallest part of the process that can execute
concurrently with other parts (threads) of the process.
A process is a heavyweight program A thread is a lightweight program.
A process can communicate with other process A thread can communicate with other thread (of the same
by using inter-process communication. process) directly by using methods like wait(), notify(),
A process has its own address space. notifyAll().
A thread uses the process’s address space and shares it with the
other threads of that process.

A process can communicate with other process A thread can communicate with other thread (of the same
by using inter-process communication. process) directly by using methods like wait(), notify(),
notifyAll().

Mekonen M. #CoSc3052  Multithreading 7


What is Multithreading?
 Multithreading in Java is a process of executing multiple threads simultaneously.
 A thread is a lightweight sub-process, the smallest unit of processing.
 Multiprocessing and multithreading, both are used to achieve multitasking.
 Multitasking is when multiple processes share common processing resources such as a CPU.
 Multi-threading extends the idea of multitasking into applications where you can subdivide
specific operations within a single application into individual threads.
 Threads use a shared memory area. They don't allocate separate memory area so saves
memory, and context-switching between the threads takes less time than process.
 Each of the threads can run in parallel.
 Java Multithreading is mostly used in games, animation, etc.

Mekonen M. #CoSc3052  Multithreading 8


Advantages of multithreading
 You can perform multiple operations together at a time, so it saves time
 Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.
 It is more responsive to user input – GUI application can interrupt a time-consuming task.
 Server can handle multiple clients simultaneously
 It is used to perform tasks in parallel processing
 A thread can execute concurrently with other threads within a single process.
 All threads managed by the JVM share memory space and can communicate with each other.

Mekonen M. #CoSc3052  Multithreading 9


Life cycle of a Thread
 There are 5 stages in the life cycle of the Thread
new  New: A new thread begins its life cycle in the new state. It
remains in this state until the program starts the thread. It is
Program starts
also referred to as a born thread.
thread  Runnable: After a newly born thread is started, the thread
becomes runnable. A thread in this state is considered to be
k executing its task.
oc l

Th
l runnable
un gna ll  Waiting: Sometimes a thread transitions to the waiting state

rea Tas
si alA

dc k
n while the thread waits for another thread to perform a task. A
si g

om
Interval
expires
ait thread transitions back to the runnable state only when
await

ple
sleep

aw ck

tes
lo another thread signals waiting thread to continue.
 Timed waiting: A runnable thread can enter the timed
timed waiting state for a specified interval of time. A thread in this
waiting terminated
waiting state transitions back to the runnable state when that time
interval expires or when the event it is waiting for occurs.
 Terminated: A runnable thread enters the terminated state
when it completes its task or otherwise terminates.

Mekonen M. #CoSc3052  Multithreading 10


Creating a Thread in Java
 There are two ways to create a Thread
1. extending the Thread class(java.lang.Thread)
2. implementing the Runnable interface(java.lang.Runnable)

Mekonen M. #CoSc3052  Multithreading 11


1) Extending Thread Class
 One way to create a thread is to create a new class that extends Thread, that is found in
java.lang.Thread and then to create an instance of that class.
 The extending class must override the run() method, which is the entry point for the new
thread and available in Thread class.
 It must also call start( ) to begin execution of the new thread.
class NewThread extends Thread{ class ExtendThread {
public void run() { public static void main(String args[]) {
try { NewThread t1 = new NewThread ();
for (int i = 5; i > 0; i--) { t1.start();
System.out.println("Child Thread: " + i); NewThread t2 = new NewThread ();
Thread.sleep(500); t2.start();
}
} catch (InterruptedException e) { }
System.out.println("Child interrupted."); }
}
System.out.println("Exiting child thread.");
}}

Mekonen M. #CoSc3052  Multithreading 12


Cont…
 The Thread class contains the constructors for creating threads for tasks and the methods for
controlling threads.

Mekonen M. #CoSc3052  Multithreading 13


Cont…

Mekonen M. #CoSc3052  Multithreading 14


2) Implementing Runnable Interface
 To implement thread using Runnable interface, Runnable interface needs to be
implemented by the class.
class NewThread implements Runnable
 Class which implements Runnable interface should override the run() method which
containts the logic of the thread.
public void run( )
 Instance of Thread class is created using following constructor.
Thread(Runnable threadOb, String threadName);
 Here threadOb is an instance of a class that implements the Runnable interface and the
name of the new thread is specified by threadName.
 start() method of Thread class will invoke the run() method.

Mekonen M. #CoSc3052  Multithreading 15


Cont…

java.lang.Runnable TaskClass // Client class


public class Client {
...
// Custom task class public void someMethod() {
public class TaskClass implements Runnable { ...
... // Create an instance of TaskClass
public TaskClass(...) { TaskClass task = new TaskClass(...);
...
} // Create a thread
Thread thread = new Thread(task);
// Implement the run method in Runnable
public void run() { // Start a thread
// Tell system how to run custom thread thread.start();
... ...
} }
... ...
} }

Mekonen M. #CoSc3052  Multithreading 16


Example Runnable Interface
class NewThread implements Runnable{ class ImplentedThread {
public void run() { public static void main(String args[]) {
try { Runnable r1 = new NewThread();
for (int i = 5; i > 0; i--) { Thread t1 = new Thread(t1);
System.out.println("Child Thread: " + i); T1.start();
Thread.sleep(500);
} }
} catch (InterruptedException e) { }
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}}

Mekonen M. #CoSc3052  Multithreading 17


Example
• The program in the next slide creates/performs three tasks and runs three threads:
▫ The first thread prints the letter a 100 times.
▫ The second thread prints the letter b 100 times.
▫ The third thread prints the integers 1 through 100.

Mekonen M. #CoSc3052  Multithreading 18


public class TaskThreadDemo {
public static void main(String[] args) {
// Create tasks
Runnable printA = new PrintChar('a' , 100);
Runnable printB = new PrintChar('b' , 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);

// Start threads
thread1.start();
thread2.start();
thread3.start();
}
} Mekonen M. #CoSc3052  Multithreading 19
class PrintChar implements Runnable {
private char charToPrint;
private int times;
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
public void run() {
for (int i = 0; i < times; i++) {
System.out.print(charToPrint);
}
}
}
class PrintNum implements Runnable{
private int lastNum;
public PrintNum(int n) {
lastNum = n;
}
public void run() {
for (int i = 1; i <= lastNum; i++) {
System.out.print(" " + i);
}
}
}

Mekonen M. #CoSc3052  Multithreading 20


Code description
 The program creates three tasks .
 To run them concurrently, three threads are created.
 The start() method is invoked to start a thread that causes the run()
method in the task to be executed.
 When the run() method completes, the thread terminates.
 Because the first two tasks, printA and printB, have similar functionality, they can be defined in one task class
PrintChar.
 The PrintChar class implements Runnable and overrides the run() method with the print-character action.
 This class provides a framework for printing any single character a given number of times.
 The runnable objects printA and printB are instances of the PrintChar class.
 The PrintNum class implements Runnable and overrides the run()
method with the print-number action.
 This class provides a framework for printing numbers from 1 to n, for any integer n. The runnable object
print100 is an instance of the class printNum class.

Mekonen M. #CoSc3052  Multithreading 21


Thread priority
 Each thread have a priority. Priorities are represented by numbers ranging from 1 and 10.
 You can increase or decrease the priority of any thread by using the
setPriority method, and you can get the thread’s priority by using the getPriority method.
 The Thread class has three int constant priorities.
 public static int MIN-PRIORITY --- with value 1
 public static int NORM-PRIORITY --- with value 5
 public static int MAX-PRIORITY --- with value 10
 Default priority of a thread is 5 (NORM-PRIORITY).
 The JVM always picks the currently runnable thread with the highest priority. A lower priority thread can run
only when no higher-priority threads are running.
 If all runnable threads have equal priorities, each is assigned an equal portion of the CPU time in a circular
queue. This is called round-robin scheduling.
 For example, suppose you insert the following code in TaskThreadDemo.java for t1.
t1.setPriority(Thread.MAX-PRIORITY);

Mekonen M. #CoSc3052  Multithreading 22


Thread scheduling
 An operating system’s thread scheduler determines which thread runs next.
 Most operating systems use time slicing for threads of equal priority.
 Thread scheduler in java is the part of the JVM that decides which thread should run.
 There is no guarantee that which runnable thread will be chosen to run by the thread
scheduler.
 Only one thread at a time can run in a single process.
 The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the
threads.
 Preemptive scheduling: the highest priority task executes until it enters a higher priority task
comes into existence. when a thread of higher priority enters the running state, it preempts the
current thread.
 Time slicing: a task executes for a predefined slice of time and then reenters the pool of ready
tasks.

Mekonen M. #CoSc3052  Multithreading 23


Starvation or Contention
 Starvation: Higher-priority threads can postpone (possible forever) the execution of lower-
priority threads.
 A thread may never get a chance to run if there is always a higher-priority thread running or a
same-priority thread that never yields.
 This situation is known as contention or starvation.

 To avoid contention, the thread with higher priority must periodically invoke the sleep or
yield method to give a thread with a lower or the same priority
a chance to run.

Mekonen M. #CoSc3052  Multithreading 24


Java Thread Pool
 Thread pool can be used to execute tasks efficiently
 Defining tasks and threads independently is convenient for a single task execution, but it is
not efficient for a large number of tasks, because you have to create a thread for each task.

 Starting a new thread for each task could limit throughput and cause poor performance.
 A thread pool is ideal to manage the number of tasks executing concurrently.
 A thread pool reuses previously created threads to execute current tasks and offers a solution
to the problem of thread cycle overhead and resource thrashing.
 Java provides the Executor interface for executing tasks in a thread pool and the
ExecutorService interface for managing and controlling tasks.

Mekonen M. #CoSc3052  Multithreading 25


Thread using Executor Framework
 Steps to execute thread using Executor Framework are as follows:
1. Create a task (Runnable Object) to execute
2. Create Executor Pool using Executors
3. Pass tasks to Executor Pool
4. Shutdown the Executor Pool

Mekonen M. #CoSc3052  Multithreading 26


Example Executable Framework
class Task implements Runnable { import java.util.concurrent.*;
private String name;
public Task(String s) { public class ExecutorThreadDemo {
name = s; public static void main(String[] args) {
} Runnable r1 = new Task("task 1");
public void run() { Runnable r2 = new Task("task 2");
try { Runnable r3 = new Task("task 3");
for (int i = 1; i<=5; i++) { Runnable r4 = new Task("task 4");
System.out.println(name+ Runnable r5 = new Task("task 5");
" - task number - "+i); ExecutorService pool =
Thread.sleep(1000); Executors.newFixedThreadPool(3);
} pool.execute(r1);
System.out.println(name+" pool.execute(r2);
complete"); pool.execute(r3);
} pool.execute(r4);
catch(InterruptedException e) { pool.execute(r5);
e.printStackTrace(); pool.shutdown();
} }
} }
}

Mekonen M. #CoSc3052  Multithreading 27


Thread Synchronization
 When we start two or more threads within a program, there may be a situation when multiple
threads try to access the same resource and finally they can produce unforeseen result due to
concurrency issues.
 For example, if multiple threads try to write within a same file then they may corrupt the data
because one of the threads can override data or while one thread is opening the same file at
the same time another thread might be closing the same file.
 So there is a need to synchronize the action of multiple threads and make sure that only one
thread can access the resource at a given point in time.
 Java programming language provides a very handy way of creating threads and
synchronizing their task by using synchronized methods & synchronized blocks.

Mekonen M. #CoSc3052  Multithreading 28


Problem without synchronization (Example)
class Table { class MyThread1 extends Thread {
void printTable(int n) { Table t;
for (int i = 1; i <= 5; i++) { MyThread1(Table t) {
System.out.print(n * i + " "); this.t = t;
try { }
Thread.sleep(400); public void run() {
} catch (Exception e) { t.printTable(5);
System.out.println(e); }
} }
}
}
}

class MyThread2 extends Thread { public class TestSynchronization {


Table t; public static void main(String args[]){
MyThread2(Table t) { Table obj = new Table();
this.t = t; MyThread1 t1 = new MyThread1(obj);
} MyThread2 t2 = new MyThread2(obj);
public void run() { t1.start();
t.printTable(100); t2.start();
} }
} }
Mekonen M. #CoSc3052  Multithreading 29
Solution with synchronized method
class Table { class MyThread1 extends Thread {
synchronized void printTable(int n) { Table t;
for (int i = 1; i <= 5; i++) { MyThread1(Table t) {
System.out.print(n * i + " "); this.t = t;
try { }
Thread.sleep(400); public void run() {
} catch (Exception e) { t.printTable(5);
System.out.println(e); }
} }
}
}
}

class MyThread2 extends Thread { public class TestSynchronization {


Table t; public static void main(String args[]){
MyThread2(Table t) { Table obj = new Table();
this.t = t; MyThread1 t1 = new MyThread1(obj);
} MyThread2 t2 = new MyThread2(obj);
public void run() { t1.start();
t.printTable(100); t2.start();
} }
} }
Mekonen M. #CoSc3052  Multithreading 30
Solution with synchronized blocks
class Table { class MyThread1 extends Thread {
void printTable(int n) { Table t;
for (int i = 1; i <= 5; i++) { MyThread1(Table t) {
System.out.print(n * i + " "); this.t = t;
try { }
Thread.sleep(400); public void run() {
} catch (Exception e) { synchronized (t) {
System.out.println(e); t.printTable(5);
} }
} }
} }
}
class MyThread2 extends Thread {
Table t;
MyThread1(Table t) { public class TestSynchronization {
this.t = t; public static void main(String args[]){
} Table obj = new Table();
public void run() { MyThread1 t1 = new MyThread1(obj);
synchronized (t) { MyThread2 t2 = new MyThread2(obj);
t.printTable(100); t1.start();
} t2.start();
} }
} }
Mekonen M. #CoSc3052  Multithreading 31
DeadLock
 Deadlock in java is a part of multithreading. Deadlock can occur in a situation when a thread
is waiting for an object lock, that is acquired by another thread and second thread is waiting
for an object lock that is acquired by first thread. Since, both threads are waiting for each
other to release the lock, the condition is called deadlock.
 Consider the scenario with two threads and two objects, as shown in Figure below. Thread 1
has acquired a lock on object1, and Thread 2 has acquired a lock on object2. Now Thread 1
is waiting for the lock on object2, and Thread 2 for the lock on object1. Each thread waits
for the other to release the lock it needs, and until that happens, neither can continue to run.

Mekonen M. #CoSc3052  Multithreading 32


Preventing DeadLock
 Deadlocks can be easily avoided by using a proper resource ordering.
 With this technique, assign an order on all the objects whose locks must be acquired and
ensure that the locks are acquired in that order.
 For the above example, suppose that the objects are ordered as object1 and object2.
 Using the resource ordering technique, Thread 2 must acquire a lock on object1 first, then on
object2.
 Once Thread 1 acquires a lock on object1, Thread 2 has to wait for a lock on object1. Thus,
Thread 1 will be able to acquire a lock on object2 and no deadlock will occur.

Mekonen M. #CoSc3052  Multithreading 33


Question?
?
Mekonen M. #CoSc3052  Multithreading 34

You might also like