Docsity

Multithreading in Java: Performing Multiple Tasks Concurrently - Prof. Nelson Padua-Perez, Study notes of Computer Science

Partial preview of the text

Download Multithreading in Java: Performing Multiple Tasks Concurrently - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

Multithreading in Java

Nelson Padua-Perez

Bill Pugh

Department of Computer Science

University of Maryland, College Park

Problem

Multiple tasks for computer

Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files to disk Perform useful computation (editor, browser, game)

How does computer do everything at once?

Multitasking Multiprocessing

Multitasking Can Aid Performance

Single task

Two tasks

Multiprocessing (Multithreading)

Approach

Multiple processing units (multiprocessor) Computer works on several tasks in parallel Performance can be improved 4096 processor Cray X 32 processor Pentium Xeon Dual-core AMD Athlon X

Perform Multiple Tasks Using…

Thread

Definition – sequentially executed stream of instructions Shares address space with other threads Has own execution context Program counter, call stack (local variables) Communicate via shared access to data Multiple threads in process execute same program Also known as “lightweight process”

Web Server uses threads to handle … Multiple simultaneous web browser requests Motivation for Multithreading

Captures logical structure of problem

May have concurrent interacting components Can handle each component using separate thread Simplifies programming for problem

Example

Multithreading Overview

Motivation & background

Threads

Creating Java threads Thread states Scheduling

Synchronization

Data races Locks Wait / Notify

Programming with Threads

Concurrent programming

Writing programs divided into independent tasks Tasks may be executed in parallel on multiprocessors

Multithreading

Executing program with multiple threads in parallel Special form of multiprocessing

Thread Class public class Thread { public Thread(Runnable R); // Thread ⇒ R.run() public Thread(Runnable R, String name); public void start(); // begin thread execution ... }

More Thread Class Methods public class Thread { … public String getName(); public void interrupt(); public boolean isAlive(); public void join(); public void setDaemon(boolean on); public void setName(String name); public void setPriority(int level); public static Thread currentThread(); public static void sleep(long milliseconds); public static void yield(); }

Alternative (Not Recommended)

Directly extend Thread class

public class MyT extends Thread { public void run() { … // work for thread } } MyT t = new MyT(); // create thread t.start(); // begin running thread

Why not recommended?

Not a big problem for getting started

but a bad habit for industrial strength development

The methods of the worker class and the

Thread class get all tangled up

Makes it hard to migrate to Thread Pools and

other more efficient approaches

Threads – Thread States State diagram new runnable terminated blocked new start terminate IO, sleep, join, request lock IO complete, sleep expired, join complete, acquire lock

Threads – Scheduling

Scheduler

Determines which runnable threads to run Can be based on thread priority Part of OS or Java Virtual Machine (JVM) Many computers can run multiple threads simultaneously (or nearly so)