







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!








] Thread ID
] Machine state (registers, program counter, stack pointer)
] Priority
] Signal mask
] Error designation (e.g., errno)
Upside:
] Cheaper to create than processes
] Cheaper to switch between than processes
] Easier to share data
Downside:
] Less robust against programming errors
] Have to share resources allocated to the execution environment
] Thread management: \ creation \ destruction \ changing priority or other attributes
] Synchronization \ join \ condition variables \ locks
] Each thread is a schedulable entity
] Threads compete for process resources on system-wide basis
] Can take advantage of multiple processor
] Operating system must provide support
] Context switch involves the kernel and can be expensive
] Run on top of existing operating systems
] Encapsulated with processes and invisible to kernel
] Schedule by a runtime system that is part of process code
] Each library function and system call is enclosed by a jacket to let the run-time library i
] Application contains user-level scheduler to manage threads within a process
] Kernel allocates virtual processors for a process
] Kernel makes upcalls to notify application’s thread scheduler of events: \ Virtual processor allocated \ Scheduler activation blocked \ Scheduler activation unblocked \ Scheduler application preempted
public class SimpleOutputThread extends Thread { String msg; int repetition;
public SimpleOutputThread(String msg, int repetition) { this.msg = msg; this.repetition = repetition; }
public void run() { for (int i = 0; i < repetition; i++) System.out.println("[" + i + "]" + msg); } }
public class SimpleOutputThreadMain { public static void main(String[] args) { SimpleOutputThread t1 = new SimpleOutputThread("Thread 1", 100); t1.start(); SimpleOutputThread t2 = new SimpleOutputThread("Thread 2", 100); t2.start(); } }
When are the thread objects created? When do they run? When does the program finish? Are there any sharing conflicts between the threads?
] A process starts with a single non-daemon thread that calls a main method. ] The process exits when either: \ Runtime.exit is called (as long as the security manager doesn't object), or \ All non-daemon threads have died (run complete or exception thrown).
] A thread object exists when it is constructed, but it doesn't start running until the start method is called. ] A thread completes (or dies) when its run method finishes or when it throws an exception. The object representing this thread can still be accessed. You must explicitly call start to run a thread.
public class SimpleOutputRunnable implements Runnable { String msg; int repetition; public SimpleOutputRunnable(String msg, int repetition) { this.msg = msg; this.repetition = repetition; } public void run() { for (int i = 0; i < repetition; i++) System.out.println("[" + i + "]" + msg); } }
public class SimpleOutputRunnableMain {
public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); new Thread(r1).start(); SimpleOutputRunnable r2 = new SimpleOutputRunnable("Thread 2", 100); new Thread(r2).start(); } }
public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); new Thread(r1).start(); new Thread(r1).start(); }
public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); Thread aThread = new Thread(r1); aThread.setDaemon(true); aThread.start(); } }
Compare the behavior with and without making aThread a Daemon thread.
] Read Chapter 6 of CDK
] Read the Mars Pathfinder article
] Work on CDK questions 6.4, 6.8, 6.9, 6.10, 6.14, 6.23, 6.24, 6.