Java Threading: Multitasking and Multiprocessing in Object-Oriented Programming II, Exams of Computer Science

An introduction to multitasking and multiprocessing in java, specifically in the context of object-oriented programming ii at the university of maryland, college park. It covers the concepts of multitasking (time-sharing) and multiprocessing (multithreading), their differences, and their benefits. The document also explains the definitions and functionalities of processes and threads, and the motivation for using multithreading. It concludes with an overview of programming with threads and creating threads in java.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-pkb
koofers-user-pkb 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Threads in Java!
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
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Java Threading: Multitasking and Multiprocessing in Object-Oriented Programming II and more Exams Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Threads in Java

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…

2. 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”

Motivation for Multithreading

1. Captures logical structure of problem

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

Example

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

Multithreading Overview

Motivation & background

Threads

Creating Java threads Thread states Scheduling

Synchronization

Data races Locks Deadlock

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 extends Object implements Runnable { public Thread(); public Thread(String name); // Thread name public Thread(Runnable R); // Thread ⇒ R.run() public Thread(Runnable R, String name); public void run(); // if no R, work for thread public void start(); // begin thread execution ... }

More Thread Class Methods public class Thread extends Object { … public static Thread currentThread() public String getName() public void interrupt() public boolean isAlive() public void join() public void setDaemon() public void setName() public void setPriority() public static void sleep() 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 thread t.start(); // begin running thread … // thread executing in parallel

Alternative Thread Creation Approach

Thread Class Approach

Extend Thread class and override run method Not recommended

Example

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

Creating Threads in Java

Note

Thread starts executing only if start() is called Runnable is interface So it can be implemented by any class Required for multithreading in applets

Threads – Thread States

Java thread can be in one of these states

New – thread allocated & waiting for start() Runnable – thread can begin execution Running – thread currently executing Blocked – thread waiting for event (I/O, etc.) Dead – thread finished

Transitions between states caused by

Invoking methods in class Thread new(), start(), yield(), sleep(), wait(), notify()… Other (external) events Scheduler, I/O, returning from run()…