Multithreading in Java - Object-Oriented Programming II - Notes | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 2006;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-wqr-1
koofers-user-wqr-1 🇺🇸

10 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Multithreading in Java
CMSC 132
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Multithreading in Java - Object-Oriented Programming II - Notes | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

Multithreading in Java

CMSC 132

Department of Computer Science

University of Maryland, College Park

Overview

Multitasking Multiprocessing Motivation for multithreading Threads

Creating Java threads Thread states Scheduling

Multitasking (Time-Sharing)

Approach

Computer does some work on a task Computer then quickly switch to next task Tasks managed by operating system (scheduler)

Computer

seems

to work on tasks concurrently

Can improve performance by reducing waiting

Multitasking Can Aid Performance Single task Two tasks

Perform Multiple Tasks Using…

Process

Definition – executable program loaded in memory Has own address space

Variables & data structures (in memory) Each process may execute a different program Communicate via operating system, files, network May contain multiple threads

Perform Multiple Tasks Using…

Thread

Definition – sequentially executed stream ofinstructions 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”

Multiple simultaneousweb browser requests…

Handled faster by multiple web servers

Motivation for Multithreading

Better utilize hardware resources

When a thread is delayed, compute other threads Given extra hardware, compute threads in parallel Reduce overall execution time

Example

Multithreading Overview

Motivation & background Threads

Creating Java threads Thread states Scheduling

Synchronization

Data races Locks Wait / Notify

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

Creating Threads in Java

Example

public class MyT implements Runnable {

public void run() {

// work for thread

} Thread t = new Thread(new MyT()); // create threadt.start();

// begin running thread

// thread executing in parallel

Java Thread Example

public class ThreadExample implements Runnable {

public void run() {

for (int i = 0; i < 3; i++)

System.out.println(i);

} public static void main(String[] args) {

new Thread(new ThreadExample()).start();new Thread( new ThreadExample()).start();System.out.println("Done"); } }

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 theThread class get all tangled up Makes it hard to migrate to Thread Pools andother more efficient approaches