

















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: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 2006;
Typology: Study notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Overview
Multitasking Multiprocessing Motivation for multithreading Threads
Creating Java threads Thread states Scheduling
Multitasking (Time-Sharing)
Computer does some work on a task Computer then quickly switch to next task Tasks managed by operating system (scheduler)
Multitasking Can Aid Performance Single task Two tasks
Perform Multiple Tasks Using…
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…
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
When a thread is delayed, compute other threads Given extra hardware, compute threads in parallel Reduce overall execution time
Multithreading Overview
Creating Java threads Thread states Scheduling
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
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)
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?
but a bad habit for industrial strength development