“Systemprogrammering” Lecture goal: Overview: ¸ Learn about the ¸ Execution context basics of ¸ Sharing computation resources concurrency in ¸ Threads in Java computer-programs o Threads? o Especially in Java o The Life cycle of a thread programs o Thread priority o Sharing resources • Syncronizing 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 1 Shared resources 1/3 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 2 Shared resources 2/3 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 3 1
Shared resources 3/3 ¸ Competition ¸ Deadlock ¸ Fairness 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 4 Concurrency… ¸ The question is how to ¸ Several users on one fix so several things to computer happen simultaneously on ¸ One user running several one computer programs on one computer o Mail client o Excel ¸ Operating systems ¸ Foreground/background 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 5 Concurrency… ¸ Several CPUs mounted in ¸ Sharing one CPU one Computer o Processes/Threads o Supercomputers o Time slices • Thousands of CPUs o Priority queues o Desktop computers • 1-4 CPUs 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 6 2
Concurrency - Process… ¸ A Program in execution Task o Memory space Job o ¸ A process has: Process A a virtual address space o program counter o executable code o data o Process C a base priority o execution stack o Process B registers o 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 7 Concurrency - Threads… ¸ A thread is a single sequential flow of control within a program Lightweight processes o Execution context o ¸ All threads of a process share its virtual address space and system resources A Program/process ¸ Threads has: A Thread Registers o Program counter o Execution Stack o 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 8 Concurrency… A Program Two Threads A Thread 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 9 3
Threads - how to in Java ¸ Subclassing the thread class and override the run-method o Works for stand alone apps ¸ Implementing the Runnable Interface 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 10 Threads - subclassing 1/3 1. public class SimpleThread extends Thread { 2. public SimpleThread(String str) { 3. super(str); 4. } 5. public void run() { 6. for (int i = 0; i < 5; i++) { 7. System.out.println(i + " " + getName()); 8. try { 9. sleep((long)(Math.random() * 1000)); 10. } catch (InterruptedException e) {} 11. } 12. System.out.println("DONE! " + getName()); 13. } 14.} 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 11 Threads - subclassing 2/3 1. public class TwoThreadsDemo { 2. public static void main (String[] args) { 3. new SimpleThread("Jamaica").start(); 4. new SimpleThread("Fiji").start(); 5. } 6. } 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 12 4
Threads - subclassing 3/3 0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 4 Fiji 3 Jamaica DONE! Fiji 4 Jamaica DONE! Jamaica 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 13 Threads - Impl. Runnable 1/2 import java.awt.Graphics; import java.util.*; import java.text.DateFormat; import java.applet.Applet; public class Clock extends Applet implements Runnable { private Thread clockThread = null; public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 14 Threads - Impl. Runnable 2/2 public void paint(Graphics g) { // get the time and convert it to a date Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); // format it and display it DateFormat dateFormatter = DateFormat.getTimeInstance(); g.drawString(dateFormatter.format(date), 5, 10); } // overrides Applet's stop method, not Thread's public void stop() { clockThread = null; } } 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 15 5
Threads - Rule of Thumb ¸ Subclassing or Implementing the Runnable int? If your class must subclass some other class, you should use Runnable alternative otherwise use subclassing ¸ Why?? 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 16 Threads - the life cycle ¸ Creating ¸ Starting ¸ Not Runnable ¸ Stopping 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 17 Threads - Creating a Thread public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } ¸ Empty thread object o No resources allocated o Only start is possible 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 18 6
Threads - Starting a Thread public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } ¸ Creates system resources ¸ Schedules the thread to run ¸ Calls the thread’s run- method ¸ Returns “running” ¸ All threads can’t run simultaneously 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 19 Threads - running public void run() { Thread myThread = Thread.currentThread(); while ( clockThread == myThread ) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to sleep anymore, // so get back to work } } } ¸ May be waiting for its turn in a pqueue 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 20 Threads - Making a Thread Not Runnable public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to sleep anymore, // so get back to work } } ] ¸ sleep(x) o Waiting x milliseconds ¸ wait() o notify() or notifyall() ¸ IO-blocked 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 21 7
Threads - Stopping a Thread 1/2 public void run() { int i = 0; while (i < 100) { i++; System.out.println("i = " + i); } } ¸ The run method terminates 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 22 Threads - Stopping a Thread 2/2 public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { … } } public void stop() { // applets' stop method clockThread = null; } 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 23 Thread Priority - sharing CPUs ¸ Java supports, a fixed ¸ A thread: priority scheduling o Inherits its priority o Relative priority o Modify • Highest first • setpriority(…) • Round robin – MIN_PRIORITY – MAX_ PRIORITY – Equal priority o Executes until o Preemptive • Higher priority runnable o Selfish threads but • Its run method exits • Avoids starvation • Time-slice ends o Time slicing not • It yields granted – only to threads at the same priority level 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 24 8
Threads- shared resources ¸ Producer/consumer ¸ Examples situation o Event queues in OS o A consumer can’t o Distributed data bases consume from an empty ¸ Java can block critical resource sections o A producer can’t o The syncronized keyword produce into a full resource ¸ Fairness - must avoid o Critical sections o Starvation • Code segments that o Deadlock access the same object 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 25 Threads- locking objects ¸ Java uses monitors ¸ Semaphores is an alternative possible to implement with monitors 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 26 Threads- locking objects example (producer) public class Producer extends Thread { private CubbyHole cubbyhole; private int number; public Producer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { for (int i = 0; i < 10; i++) { cubbyhole.put(i); System.out.println("Producer #" + this.number + " put: " + i); try { sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } } } } 12/9-02 Datorkommunikation & Internet, Anders Broberg, Umu - Systemprogrammering 27 9
Recommend
More recommend