



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!




What is a thread?
Thread state:
Why threads?
Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3
Figure 6. Client and server with threads
Server
N threads
Input-output
Client
Thread 2 makes
T
Thread 1
requests to server
generates results Requests
Receipt & queuing
Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3
Figure 6. Alternative server threading architectures (see also Figure 6.5)
a. Thread-per-request b. Thread-per-connection c. Thread-per-object
remote
workers I/O (^) remote I/O remote
per-connection threads per-object threads
objects objects^ objects
Thread operations:
\ creation \ destruction \ changing priority or other attributes
\ join \ condition variables \ locks
Kernel-level threads:
User-level threads:
Hybrid approaches:
\ Virtual processor allocated \ Scheduler activation blocked \ Scheduler activation unblocked \ Scheduler application preempted
Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3
Figure 6. Scheduler activations
Process A
Process B
Virtual processors Kernel
Process Kernel
P idle P needed
P added
SA blocked
SA unblocked
SA preempted
A. Assignment of virtual processors to processes
B. Events between user-level scheduler & kernel Key: P = processor; SA = scheduler activation
Examples of different approaches:
Implementing the Runnable interface:
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); } }
Starting Runnable objects in separate threads
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(); } }
Starting the same Runnable in separate threads
public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); new Thread(r1).start(); new Thread(r1).start(); }
Making a Daemon Thread
public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); Thread aThread = new Thread(r1); aThread.setDaemon(true); aThread.start(); } }
Extending Thread versus implementing Runnable
Thread versus simple method call:
void *doit(void *) a.doit(x );
void *doit(void *) pthread_create( , , &doit, x);
For next time: