








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!









cs480 (Prasad) L11Threads 1
cs480 (Prasad) L11Threads 2
cs480 (Prasad) L11Threads 5
cs480 (Prasad) L11Threads 6
Alternative :Creating and Running
Threads
instance of a class implementing the
cs480 (Prasad) L11Threads 13
interface Runnable.
Example using interface Runnable
class Echo implements Runnable { int id; Echo(int i) { id = i; } public void run() { while (true) { System.out.println(id +“ ABC ”); // yield();
cs480 (Prasad) L11Threads 14
// yield(); } } public static void main (String[] args) { new Thread (new Echo(1)). start(); new Thread (new Echo(2)). start(); } }
Same Example in C#
using System.Threading;
class Echo { int id; Echo(int i) { id = i; } public void run() { while (true) while (true) System.Console.WriteLine(id + " ABC "); } public static void Main () { new Thread ( new ThreadStart (new Echo(1).run)). Start(); new Thread ( new ThreadStart (new Echo(2).run)). Start(); } }
Alternate Rendition in Java
class Echo implements Runnable { public void run() { try {{ while (true) { Thread.sleep(1000); System.out.println(“ABC”); } }} catch (InterruptedException e) { }} catch (InterruptedException e) { return; } } public static void main (String[] args) { Runnable r = new Echo(); new Thread(r). start(); } }
Threads and Applets
cs480 (Prasad) L11Threads 17
A Simple Bouncing Ball Animation
cs480 (Prasad) L11Threads 18
Thread.sleep(15); repaint(); } } catch (InterruptedException e) {}; */
Threaded Applet
...
cs480 (Prasad) L11Threads 25
Mutual Exclusion
cs480 (Prasad) L11Threads 26
Sharing Data
Threads Sharing Data
class Shared { private int cnt = 0; // synchronized void print(String Id) { System.out.print(Id + “:” + cnt ); System.out.println( “-” + ++cnt); } } } class Shared { private int cnt = 0; void print(String Id) { synchronized (this) { System.out.print(Id + “:” + cnt ); System.out.println( “-” + ++cnt); } } }
Motivation for Mutual Exclusion
cs480 (Prasad) L11Threads 29
// C# Equivalent using System; using System.Threading; class Shared { private int cnt = 0; public void print(String Id) { lock (this) { Console.Write(Id + ":" + cnt); Console.WriteLine("-" + ++cnt); }; } } class SharingThread { private static Shared s = new Shared(); S i Id
cs480 (Prasad) L11Threads 30
String Id; SharingThread (String Name) { Id = Name; } public void run () { while (true) { s.print(Id); } } public static void Main (string [] args) { new Thread (new ThreadStart (new SharingThread("A").run) ). Start() ; new Thread (new ThreadStart (new SharingThread("B").run) ). Start() ; } }
Interaction of Language Features
run() method case study
Subclassing : Access Control
class Thread { ... public void run() { } } class SubThread extends Thread { public void run() { ... }
Thread t = new SubThread();
void run() { ... }
Thread Scheduling
cs480 (Prasad) L11Threads 37
Blocked Thread
cs480 (Prasad) L11Threads 38
waiting.
Thread Scheduling
highest-priority thread.
priority thread that becomes runnable. p y
lower priority thread, to avoid starvation.
run in round-robin fashion.
Scheduling Ambiguity
schedule same priority threads.
(“Fair policy”)
same priority threads to run until the thread
voluntarily relinquishes the CPU (by
sleeping, yielding, or exiting), or
until a higher priority thread preempts it.
Cooperative Concurrent Threads
Producer-Consumer Problem
cs480 (Prasad) L11Threads 41
Producer-Consumer Problem
Bounded-Buffer Problem
(Clients and Servers : Resource Sharing)
Simplistic 1-Cell Buffer
cs480 (Prasad) L11Threads 42
class Producer extends Thread { private Random r = new Random(); Buffer b; String id; public Producer(Buffer _b, String _id) { b = _b; id = _id; } public void run () { int delay; Integer ir; try { while (true) { while (true) { delay = Math.abs(r.nextInt() % 1999) + 50; sleep(delay); ir = new Integer(delay); System.out.println("Ready << "+id+" puts "+ ir);
}catch (Exception e){System.out.println("Exception$ " + e);}; } }
class Consumer extends Thread { private Random r = new Random(); Buffer b; String id; public Consumer(Buffer _b, String _id) { b = _b; id = _id; } public void run () { int delay; Integer ir = null; try { while (true) { delay = Math.abs(r.nextInt() % 1999) + 50; sleep(delay); System.out.println("Ready >> " + id + " gets ");
} catch (Exception e) {System.out.println("Exception$ " + e);}; } }
System.out.println("Done >> "+id+" gets "+x);
cs480 (Prasad) L11Threads 49
Ready >> C2 gets Ready >> C1 gets Ready << P1 puts 1672 Done << P1 puts 1672 Done >> C2 gets 1672 Ready << P2 puts 774 Done << P2 puts 774 Done >> C1 gets 774
Ready << P1 puts 622 Done << P1 puts 622 Ready >> C1 gets Done >> C1 gets 622 Ready >> C2 gets Ready << P2 puts 1447 D P2 t 1447
cs480 (Prasad) L11Threads 50
Done >> C1 gets 774 Ready << P1 puts 168 Done << P1 puts 168 Ready >> C1 gets Done >> C1 gets 168 Ready << P2 puts 1263 Done << P2 puts 1263 Ready >> C1 gets Done >> C1 gets 1263 …
Done << P2 puts 1447 Done >> C2 gets 1447 Ready << P2 puts 96 Done << P2 puts 96 Ready << P2 puts 95 Ready >> C1 gets Done >> C1 gets 96 Done << P2 puts 95 ...
using System; using System.Threading; class Buffer { Object x; bool empty = true; public void put (Object _x, string id) { lock(this){ while (!empty) Monitor.Wait(this); empty =! empty; Console.WriteLine("Done << " + id + " puts " + _x); x = _x; Monitor.PulseAll(this); } } public Object get (string id) { lock(this){ while (empty) Monitor.Wait(this); empty =! empty; Console.WriteLine("Done >> " + id + " gets " + x); Monitor.PulseAll(this); return x; } } }
class Producer { private System.Random r = new System.Random(); Buffer b; String id; public Producer(Buffer _b, String _id) { b = _b; id = _id; } public void run () { int delay; try {
cs480 (Prasad) L11Threads 53
while (true) { delay = r.Next(2000) + 50; Thread.Sleep(delay); Console.WriteLine("Ready << " + id + " puts " + delay); b.put(delay, id); } } catch (Exception e){Console.WriteLine("Exception$ " + e);}; } }
class Consumer { private System.Random r = new System.Random(); Buffer b; String id; public Consumer(Buffer _b, String _id) { b = _b; id = _id; } public void run () { int delay; try {
cs480 (Prasad) L11Threads 54
while (true) { delay = r.Next(2000) + 50; Thread.Sleep(delay); Console.WriteLine("Ready >> " + id + " gets "); delay = (int) b.get(id); } } catch (Exception e) {Console.WriteLine("Exception$ " + e);}; } }
public class PCB {
public static void Main(string[] args) {
Buffer b = new Buffer();
new Thread( new ThreadStart(new Consumer(b,"C1").run)). Start();
new Thread( new ThreadStart(new Producer(b,"P1").run)). Start();
try {Thread.Sleep(1000);}
cs480 (Prasad) L11Threads 55
y p catch (Exception e){Console.WriteLine("Exception$ " + e);};
new Thread( new ThreadStart(new Consumer(b,"C2").run)). Start();
new Thread( new ThreadStart(new Producer(b,"P2").run)). Start();
}
}