0% found this document useful (0 votes)
1 views34 pages

Week11 Thread

This document provides an overview of concurrent programming in Java, focusing on threads and their management. It explains the concepts of multitasking and multithreading, advantages of multithreading, and the life cycle of a thread, including methods for creating and managing threads. Additionally, it discusses concurrency and parallelism, threading mechanisms, thread methods, and thread priority, highlighting their importance in improving application performance.

Uploaded by

bq4xx42p8w
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)
1 views34 pages

Week11 Thread

This document provides an overview of concurrent programming in Java, focusing on threads and their management. It explains the concepts of multitasking and multithreading, advantages of multithreading, and the life cycle of a thread, including methods for creating and managing threads. Additionally, it discusses concurrency and parallelism, threading mechanisms, thread methods, and thread priority, highlighting their importance in improving application performance.

Uploaded by

bq4xx42p8w
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
You are on page 1/ 34

CMPE 261

Large Scale Programming

Dr. Emel Küpçü


Department of Computer Engineering
İstanbul Bilgi University
Week - 11
Java: Introduction to
Concurrent
Programming, Threads

Dr. Emel Küpçü


Department of Computer
Engineering
İstanbul Bilgi University
Thread

• Refers to the smallest unit of execution within a process.


• It is a sequence of instructions that can be managed independently by
a scheduler, which is part of the OS.
• It is a lightweight subprocess,
• Consumes fewer system resources.
• Can be created and destroyed more quickly.

• It is a separate path of execution.


• It is executed inside a process. There can be multiple processes inside
the OS and one process can have multiple threads.
Threads

• Threads are commonly used in applications that require parallel


processing, such as web servers, GUI applications, and real-time
systems.
• Multiple threads can exist within the same process while running
concurrently.
• Run concurrently either asynchronously or synchronously.
• Improve the performance of applications by performing tasks in parallel.
• Share resources such as the same memory,
• allows for efficient communication between threads.
Multitasking and Multithreading
• Multitasking
• Refers to a computers ability to perform Word
Processor
multiple jobs concurrently.
• More than one program are running
concurrently. Web Browser OS CPU


• Word processor, web browser, antivirus
software
Antivirus
• Multithreading
• Refers to multiple threads of control
within a single program,
Thread 1
Graphic
• Each program can run multiple threads
of control within the program, Responding Thread 2 Word
to Keystrokes Processor
CPU
• Word Processor
Grammar Thread 3
Check
Advantages of Multithreading

• It reduces the computation time.


• It improves performance of an application.
• Threads share the same memory space, which helps save memory.
• Context switching between threads is usually less costly than between
processes.
• Cost of communication between threads is comparatively low.
CPU Core
• A CPU core is a processing unit within a computer's central processing unit
(CPU) that executes instructions.
• Modern CPUs typically contain multiple cores (multi-core processors), each capable of
executing tasks independently.
• The more cores a CPU has, the better it can handle simultaneous operations,
improving performance in tasks like multitasking or running multiple applications.
• When a CPU has multiple cores, it can run multiple threads in parallel, making it well-
suited for multi-threaded applications (like complex calculations, video editing, etc.).
• Each core can handle a separate instruction or a group of instructions at the same time,
speeding up computation.

• Example:
• A 4-core CPU can process tasks simultaneously, such as running four different
applications at once, like web browsing, video playback, background music, and an
antivirus scan.
• A single-core CPU can only handle one of these tasks at a time, even if it switches
between them quickly.
Concurrency and Parallelism
• In a concurrent system, multiple tasks can start,
Single run, and complete in overlapping time frames, giving
Core
Time the illusion that they are running simultaneously.
Start
Task
• Concurrency is typically achieved by time-slicing
Task1.1
• CPU rapidly switches between different tasks for short periods
Task2.1
• It creates an interleaved execution pattern
Task 2.2
• Only one task is being actively worked on at any given
Task 1.2 moment.
Task 2.3 • Concurrency is useful when dealing with tasks that involve
waiting,
.
End
. • e.g. I/O operations (e.g., reading from a disk, network
. communication).
Task
• It helps to maximize resource utilization and improve responsiveness.
Concurrent
Concurrency and Parallelism

Single • Parallelism refers to the


Core 1 Core 2
Core
Time simultaneous execution of
Start
multiple tasks.
Task
Task1.1 • It requires multiple processing
units (Multiple CPU cores)
Task2.1
• It divides tasks into subtasks
Task 2.2
executed simultaneously on
different cores.
Task 1.2 Task1 Task 2 • It leads to a reduction in overall
execution time.
Task 2.3 • It is ideal for computationally
. intensive tasks that can be
End
. divided into independent parts,
.
Task • e.g. large-scale data processing,
scientific simulations, or rendering
graphics.
Concurrent Parallel
Concurrency and Parallelism

Single
Core 1 Core 2 Core1 Core2
Core
Time
Start
Task
Task1.1 Task 1.1 Task 2.2
Task2.1 Task 2.1 Task 1.2
Task 2.2 Task 2.4 Task 2.3
Task 1.2 Task1 Task 2
Task 1.3 Task 1.4
Task 2.3
.
.
Task 2.5 Task 2.6
End
.
Task

Concurrent Parallel Concurrent and Parallel


Examples for Real-World Systems
• Web servers:
• A web server needs to handle multiple client requests.
• Parallelism handles tasks on multiple CPU cores at the same time, while
concurrency keeps the server active by handling other tasks while waiting for
slow responses, like database queries or file loading.

• Multi-threaded applications:
• Multi-threaded applications, such as video games or interactive simulations, use
concurrent threads to handle user inputs, physics calculations, and graphics
rendering simultaneously.
• Combining concurrency with parallel processing with multiple CPU cores for
intensive tasks (like rendering) greatly improves performance and
responsiveness.
Threading Mechanisms

There are two methods for implementing threading mechanism


1. Create a class that extends the Thread class (java.lang.Thread )
2. Create a class that implements the Runnable interface
(java.lang.Runnable)
In both cases the run() method should be implemented. *
• If you extend the Thread Class, it means that subclass cannot extend any other
Class, but if you implement Runnable interface then you can do this.
• Java does not allow multiple class inheritance.
• Java allows a class to implement multiple interfaces, which is its way of handling a form of
multiple inheritance.
1. Extending Thread class
• Threads are implemented as objects that contains a method called run()
• The run() method is overridden from the Thread class.
• The run() method is the entry point for the thread's execution.

class MyThread extends Thread


{
public void run(){
// The code here runs when the thread is started.
}
}

• Create a thread:
MyThread thread1 = new MyThread();
• Start Execution of threads:
• Once you call start(), the code inside the run() method will be executed in a new thread of
execution, concurrently with other threads.
• The run() method is not called directly; instead, it is invoked when start() is called on a Thread
object.
thread1.start();
Example: Extending Thread class
• class MyThread extends Thread {
public void run()
{
System.out.println(" this thread is running ... ");
}
}
public class ThreadEx1 {
public static void main(String [] args ) {
The run() Method
MyThread t = new MyThread(); is invoked
t.start();
}
}
2. Threads by implementing Runnable interface
class MyThread implements Runnable{
public void run() {
// The code runs when the thread is started.
}
}
• Creating Object:
• The myObject contains the task logic defined in the run() method but does not represent a thread yet.
MyThread myObject = new MyThread();
• Creating Thread Object:
Thread thread1 = new Thread( myObject );
• Start Execution:
• This starts the thread execution by calling the start() method on the thread1 object.
• The start() method internally calls the run() method of the Runnable object myObject.
thread1.start();
Example: Threads by implementing Runnable interface

• class MyThread implements Runnable {


public void run() {
System.out.println(" this thread is running ... ");
}
}
public class ThreadEx2 {
public static void main(String [] args ) {
Creates the object of
MyThread class
Thread thread = new Thread(new MyThread());
t.start(); The object of Thread class
}
}
Commonly Used Constructors of Thread Class
• Thread()
• It creates a thread without a task or custom name.
• The thread has no task to execute until the run() method is overridden.
• You would typically use this constructor when you want to create a thread and later
assign it a task, either by overriding the run() method in a subclass or by using the
Runnable interface.
• Thread( String name)
• It creates a thread with a specified name, but the thread will still use the default run()
method.
• Thread(Runnable r)
• It creates a thread that executes the task defined by the Runnable object passed as an
argument.
• Thread(Runnable r, String name)
• It creates a thread that executes the task defined by the Runnable object and has a
specified name.
Life Cycle of a Thread

• During Life time of a thread there are many states it can enter.
• Newborn state
• Runnable state
• Running state
• Blocked state
• Dead state

• A thread is always in one of these five states.


• It can move from one state to another via a variety of ways.
Thread scheduler
• It is a component of the JVM that determines which thread gets access to
the CPU for execution.
• When multiple threads are ready to run (in the runnable state), the thread
scheduler decides the order and duration for which each thread is allowed
to execute.
• The thread scheduler operates based on thread priorities and scheduling
policies defined by the underlying operating system.
• However, thread scheduling in Java is largely non-deterministic, meaning it does not
guarantee any specific order of thread execution.

• In Java, developers cannot directly control or interact with the thread


scheduler. They can influence scheduling behavior to some extent using
methods like:
1. sleep(): Puts the current thread to sleep for a specified duration.
2. join(): Allows one thread to wait for the completion of another.
Life Cycle of a Thread
Thread t = new Thread();
New
start()

Runnable

run() yield() Dead Killed Thread

Notify() Running
•New → Runnable: When the start() method is called.
sleep() •Runnable → Running: When the thread is picked by the
wait()
scheduler. (e.g., waiting for I/O or calling the sleep() method).
•Running → Blocked/Waiting: When the thread needs to wait
Blocked for resources or a condition.
•Blocked/Waiting → Runnable: When the thread is ready to run
Idle Thread
again (e.g., the resource becomes available).
•Running → Dead: When the thread completes execution.
State Transition Diagram of a Thread
Thread Methods

• start()
• Starts a new thread.
• It causes the thread to move from the New state to the Runnable state, and the run()
method will be invoked by the JVM.

• run()
• It contains the code that is executed when the thread is started.
• It is the entry point for the thread's execution.
• You usually override this method when creating your own thread
Thread Methods
class ThreadSleep extends Thread {
• sleep(long milliseconds) public void run() {
for (int i = 1; i <= 5; i++) {
• This method pauses the try {
execution of the current thread Thread.sleep(1000); // Pauses
execution for 1 second
for the specified amount of time in } catch (InterruptedException e) {
milliseconds. System.out.println(e);
}
• It throws an InterruptedException System.out.println(i);
if the thread is interrupted while }
}
sleeping. }
public class Example_sleep {
Output:
public static void main(String[] args) {
Prints a number at every second:
System.out.println("Prints a number at
every second: ");
1
ThreadSleep t1 = new ThreadSleep();
2
t1.start();
3
}
4 }
5
class TreadJoin extends Thread {
Thread Methods public void run() {
for (int i = 1; i <= 5; i++) { Output:
try { 1
Thread.sleep(1000); 2
} catch (InterruptedException e) { 3
System.out.println(e); 4
• join() } 5
System.out.println(i); 1
• This method forces the current } 2
} 3
thread to wait until the thread on 4
}
which join() is called finishes its public class Example_Join { 5
execution. public static void main(String[] args) {
TreadJoin t1 = new TreadJoin();
• This is useful when you want to TreadJoin t2 = new TreadJoin();
ensure one thread finishes before t1.start();
try {
another starts or continues. t1.join(); Waits for t1 to finish before continuing
} catch (InterruptedException e) {
System.out.println(e);
}
t2.start();
}
}
Thread Methods class ThreadInterrupt extends Thread {
public void run() {
try {
for (int i = 1; i <= 10; i++) {
// Print the seconds
System.out.println("Second: " + i);
Thread.sleep(1000); // Sleep for 1 second between prints
• interrupt() }
System.out.println("Thread completed");
} catch (InterruptedException e) {
• It is used to interrupt a thread System.out.println("Thread interrupted");
}
that is either sleeping or }
}
waiting. public class Example_Interrupt {
public static void main(String[] args) {
• It sets the interrupt flag of the ThreadInterrupt t = new ThreadInterrupt();
t.start();
thread and can also throw an
try {
InterruptedException if the // After starting, we can interrupt the thread after a specific
thread is in a blocked/waiting time Main thread sleeps for 2 sec.
Thread.sleep(2000); // Main thread sleeps for 5 seconds
state (e.g., sleeping or t.interrupt(); Interrupt the sleeping thread
// Interrupt the thread after 5 seconds
waiting). } catch (InterruptedException e) {
Output: System.out.println(e);
Second: 1 }
}
Second: 2 }
Thread interrupted
Thread Methods class ThreadIsAlive extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
• isAlive()
public class Example_IsAlive {
• This method checks if
public static void main(String[] args) {
the thread is still ThreadIsAlive t = new ThreadIsAlive();
running or has finished t.start();
// Check if the thread is alive after starting
its execution. It returns
System.out.println("Is thread alive (before completion)? " + t.isAlive());
true if the thread is still
alive, otherwise false. try {
t.join(); // Wait for the thread to finish its task
} catch (InterruptedException e) {
e.printStackTrace();
}

Output: // Check if the thread is alive after completion


Thread is running... System.out.println("Is thread alive (after completion)? " + t.isAlive());
Is thread alive (before completion)? true }
Is thread alive (after completion)? false }
Thread Priority
• Every thread has a priority.
• When a thread is created, it inherits the priority of the thread that
created it.
• The priority values range from 1 to 10, in increasing priority.
• The priority can be adjusted subsequently using the setPriority()
method
• Threadname.setPriority(intNumber);
• The priority of a thread may be obtained using getPriority()
• Priority constants are defined:
• MIN_PRIORITY=1
• MAX_PRIORITY=10
• NORM_PRIORITY=5 (The main thread is created with this priority )
Thread Methods: Thread Priority
• getPriority() and setPriority(int priority)
Priorities can be set between Thread.MIN_PRIORITY (1) and Thread.MAX_PRIORITY (10), with the default
being Thread.NORM_PRIORITY (5).
class ThreadPriority extends Thread {
public void run() { Output:
// Print the priority of the thread during its execution Initial thread priority (before set): 5
Thread priority (in run method): 10
System.out.println("Thread priority (in run method): " +
Thread.currentThread().getPriority());
}
}
public class Example_Priority {
public static void main(String[] args) throws InterruptedException {
ThreadPriority t1 = new ThreadPriority()
// Display the initial priority of t1 (before setting any new priority)
System.out.println("Initial thread priority (before set): " + t1.getPriority());
t1.start();// Start the thread
// Set the highest priority after starting the thread
t1.setPriority(Thread.MAX_PRIORITY); // Set the highest priority
}
}
Deamon Thread

• It is a low priority thread (in JAVA - JVM) that runs in background to


perform tasks
• e.g. garbage collection or other housekeeping operations.

• The JVM terminates itself when all user threads (non-deamon) finish
their execution.
• The JVM does not care whether Deamon thread is running or not.
• The JVM will automatically terminate any remaining daemon threads.
Thread Methods: Deamon Thread
class ThreadDeamon extends Thread {
public void run() {
if (Thread.currentThread().isDaemon()) {
• setDaemon(boolean on) System.out.println("Daemon thread running...");
} else {
• This method sets a thread as a System.out.println("User thread running...");
daemon thread. }
}
• Daemon threads are low-priority
}
threads that run in the
background and do not prevent public class Example_Deamon {
the JVM from exiting when all public static void main(String[] args) {
user threads have finished. ThreadDeamon t1 = new ThreadDeamon();
ThreadDeamon t2 = new ThreadDeamon();
t1.setDaemon(true); // Set t1 as a daemon thread
t1.start();
t2.start();
}
}
Example: Performance of Multithreading
// Task 1: Simulate downloading a file
class DownloadTask implements Runnable {
@Override
public void run() {
System.out.println("Downloading file...");
try {
Thread.sleep(2000); // Simulate time taken to download (2 seconds)
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("File download complete.");
}
}
// Task 2: Simulate processing data
class ProcessDataTask implements Runnable {
@Override
public void run() {
System.out.println("Processing data...");
try {
Thread.sleep(3000); // Simulate time taken to process data (3 seconds)
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Data processing complete.");
}
}
Example: Multithreading (cont.)
public class ThreadVsNoThreadExample {
public static void main(String[] args) {
// 1. Measure time with threads (concurrent execution)
System.out.println("Starting concurrent execution with threads...");
long startTimeWithThreads = System.nanoTime();
DownloadTask downloadTask = new DownloadTask();
ProcessDataTask processDataTask = new ProcessDataTask();
// Create threads for each task
Thread thread1 = new Thread(downloadTask);
Thread thread2 = new Thread(processDataTask);
// Start both threads to run concurrently
thread1.start();
thread2.start();
try {// Wait for both threads to finish
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Example: Multithreading (cont.)
long endTimeWithThreads = System.nanoTime();
long timeTakenWithThreads = endTimeWithThreads - startTimeWithThreads;
System.out.println("Concurrent execution with threads completed.");
System.out.println("Time taken with threads: "+timeTakenWithThreads /1_000_000 + "
ms\n");

// 2. Measure time without threads (sequential execution)


System.out.println("Starting sequential execution without threads...");
long startTimeWithoutThreads = System.nanoTime();
// Execute tasks sequentially (one after the other)
downloadTask.run(); // Run the download task
processDataTask.run(); // Run the process data task
long endTimeWithoutThreads = System.nanoTime();
long timeTakenWithoutThreads = endTimeWithoutThreads - startTimeWithoutThreads;
System.out.println("Sequential execution without threads completed.");
System.out.println("Time taken without threads:
"+timeTakenWithoutThreads/1_000_000 + " ms");
}
}
Output
Starting concurrent execution with threads...

Downloading file...

Processing data...

File download complete.

Data processing complete.

Concurrent execution with threads completed.

Time taken with threads: 3005 ms

Starting sequential execution without threads...


Downloading file...

File download complete.

Processing data...
Data processing complete.

Sequential execution without threads completed.


Time taken without threads: 5008 ms
Questions ?

You might also like