(21) Concurrency

(21) Concurrency - Problem CMSC 132: Object-Oriented...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC 132: Object-Oriented Programming II Concurrency in Java 1 Department of Computer Science University of Maryland, College Park Problem We want our computer system to be able to perform many tasks simultaneously Draw and display images on the screen Check keyboard and mouse input Send and receive data over the network 2 Read and write files to disk Perform useful computation (edit programs, compile things) Play important games How does a computer do everything at once? Multitasking Multiprocessing Multitasking (or timesharing) A system does some work on a task, then quickly switches to the next task Tasks are managed by the operating system (scheduler) his makes the system eem work on tasks 3 This makes the system seem to work on tasks at the same time This can improve performance by reducing waiting Multitasking can increase utilization A single task: 4 Two tasks:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Multiprocessing Multiple processing units (multiprocessor) allow a system to actually work on several tasks in parallel Performance can be improved 5 4096 processor Cray X1 32 processor Pentium Xeon Dual-core AMD Athlon X2 Beowulf computer cluster (Borg, 52- node cluster used by McGill University Image/Info from Wikipedia ) A process A process is an executable program loaded in memory It has its own address space , meaning variables and data structures (in memory) Each process may be executing a different 6 program Processes communicate via the operating system, files, the network A thread A thread (also known as a lightweight process ) is a sequentially-executed stream of instructions It shares an address space with other threads Threads communicate via shared access to ata 7 data It has its own execution context, meaning program counter and runtime call stack (local variables) Multiple threads in a process execute the same program How is memory is organized for a threaded environment? 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 2. Better utilize hardware resources 8 When a thread is delayed, compute other threads Given extra hardware, work for different threads can be computed in parallel Reduce overall execution time
Background image of page 2
Concurrent programming Either multiprocessing or threads Involves concurrently-executing independent tasks Tasks may be executed in parallel on multiprocessor hardware Multithreading 9 Involves executing single programs that are written in the form of multiple threads that execute concurrently Is a special form of multiprocessing Creating threads in Java Need to specify work performed by thread Two approaches: 1. Implement the Runnable interface (the preferred approach) public interface Runnable { 10 2. Extend the Thread class public void run(); // work for thread } public class Thread extends Object { } The Thread class public class Thread extends Object implements Runnable {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

(21) Concurrency - Problem CMSC 132: Object-Oriented...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online