









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










1
2
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
3
Multitasking (Time-Sharing)
Computer does some work on a task Computer then quickly switch to next task Tasks managed by operating system (scheduler)
4
Multitasking Can Aid Performance
7
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”
8
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
9
Motivation for Multithreading
When a thread is delayed, compute other threads Given extra hardware, compute threads in parallel Reduce overall execution time
Multiple simultaneous web browser requests…
Handled faster by multiple web servers
10
Multithreading Overview
Creating Java threads Thread states Scheduling
Data races Locks Deadlock
13
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 ... }
14
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() }
15
Creating Threads in Java
Create Thread object Pass worker object to Thread constructor Or hand the worker instance to an executor Alternative methods for running threads
16
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
19
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
20
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()…
21
Threads – Thread States
runnable
scheduler
new
dead
running blocked
new start
terminate
IO, sleep, wait, join
yield, time slice
notify, notifyAll, IO complete, sleep expired, join complete
Running is a logical state → indicates runnable thread is actually running
22
Daemon Threads
User Daemon Provide general services Typically never terminate Call setDaemon() before start()
25
Threads – Preemptive Scheduling
Same reasons as non-preemptive scheduling Preempted by scheduler
26
Java Thread Example
public class ThreadExample extends Thread { public void run() { for (int i = 0; i < 3; i++) { try { sleep((int)(Math.random() * 5000)); // 5 secs } catch (InterruptedException e) { } System.out.println(i); } } public static void main(String[] args) { new ThreadExample().start(); new ThreadExample().start(); System.out.println("Done"); } }
27
Java Thread Example – Output
0,1,2,0,1,2,Done // thread 1, thread 2, main() 0,1,2,Done,0,1,2 // thread 1, main(), thread 2 Done,0,1,2,0,1,2 // main(), thread 1, thread 2 0,0,1,1,2,Done,2 // main() & threads interleaved
thread 1: println 0, println 1, println 2
main (): thread 1, thread 2, println Done
thread 2: println 0, println 1, println 2
28
Thread Class – join( ) Method
public final void join( ) Returns when thread is done Throws InterruptedException if interrupted