
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Exams
1 / 32
This page cannot be seen from the preview
Don't miss anything!

























Problem
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)
Multitasking Multiprocessing
Multitasking Can Aid Performance
Multiprocessing (Multithreading)
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
Beowulf computer cluster (Borg, 52- node cluster used by McGill University Image/Info from Wikipedia )
Perform Multiple Tasks Using…
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
May have concurrent interacting components Can handle each component using separate thread Simplifies programming for problem
Web Server uses threads to handle …
Multiple simultaneous web browser requests
Multithreading Overview
Creating Java threads Thread states Scheduling
Data races Locks Deadlock
Programming with Threads
Writing programs divided into independent tasks Tasks may be executed in parallel on multiprocessors
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
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
Extend Thread class and override run method Not recommended
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
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
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
Invoking methods in class Thread new(), start(), yield(), sleep(), wait(), notify()… Other (external) events Scheduler, I/O, returning from run()…