SlideShare a Scribd company logo
Concurrent Programming CS 3331 Fall 2007
Outline What’re threads? How to create threads? How to controlling threads? Thread safety and synchronization BoundedQueue example
Concurrent Programming Shared memory vs. message-based (e.g., IPC) Built-in language features vs. library-based
Multithreaded Programming Thread A single sequential flow of control within a program, i.e., a lightweight flow that executes concurrently with other threads within a single process. Communicates with other threads via shared variables and objects. Managed by JVM. flow of control sequential  concurrent
Why? Can take advantages of multiple CPUs. Allows a server to handle multiple clients simultaneously.  Suitable for developing reactive system. Makes application more responsive to user inputs (e.g., GUI programs such as BouncingBall applet). Natural to model real worlds.
But, More difficult to program and debug (e.g., shared access to objects) Nondeterministic order of thread execution Safety and liveness problems
Creating Threads in Java Two approaches By extending the java.lang.Thread class By implementing the java.lang.Runnable interface
Creating Threads (Cont.) Extending the Thread class public class MyThread extends Thread { public void run() { // the thread body } // other methods and fields. } // Start a new thread by calling the start method MyThread thread = new MyThread(); thread.start(); // … flow of control start() run() // … The method run is the concurrent unit in Java.
Creating Threads (Cont.) Implementing the Runnable interface public interface Runnable { void run(); } public class MyThread implements Runnable { public void run() { // the thread body } // other methods and fields. } // Start a new thread by creating an instance and calling the start method, e.g., new Thread(new MyThread()).start();
Example --- Simple Counter public class Counter extends Thead { private int count, inc, delay; public Counter(int init, int inc, int delay) { this.count = init; this.inc = inc; this.delay = delay; } public void run() { try { for (;;) { System.out.print(count + “ “); count += inc; sleep(delay); } } catch (InterruptedException e) {} } public static void main(String[] args) { new Counter(0, 1, 33).start(); new Counter(0, -1, 100).start(); } }
Example (Cont.) Output: 0 0 1 2 -1 3 4 5 -2 6 7 8 -3 9 10 -4 11 12 13  -5 14 15 16 -6 17 18 -7 19 20 21 -8 22 23 24 -9 25 26 -10 27 28 -11 29 30 31 -12 32 33 34 -13 35 36 37 -14 38 39 -15 40 41 42 -16 43 44 45 … Q: What will happen if we change the statement “new Counter().start()” to “new Counter().run()” in the main method?
Exercise Write a thread class named Talker that prints a hello message, say “Hello, I am Tom!”, continuously, and create two instances as separate threads.  public class Talker _______________________ { private String name; public Talker(String name) {  this.name = name;  } }
Controlling Threads Life cycle of threads New After creation but before start() call Alive After the start() call; run() is implicitly called. Consists of two sub states Runnable : Ready to run; maybe running or waiting for their turns Non-runnable : Not ready to run; waiting for some events (e.g., sleeping, blocked, or waiting) Dead When run() method returns start Non-runnable New Dead Runnable yield sleep join end of run Alive
Methods of Thread Class Method  Description start() Enter the  Runnable  state from the  new  state  *sleep() Enter the  Non-runnable  state join() Enter the  Non-runnable  state and wait for another thread to terminate *yield() Release CPU and remain in the  Runnable interrupt() Cause InterruptedException isAlive() True if in the  alive  state isInterrupted() True if interrupted * static method
Example Coordinating concurrent jobs public class Job extends Thread { public void run() {  // do some meaningful work, e.g.,  writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); } for (int i = 0; i < jobs.length; i++) { jobs[i].join(); } } } jobs[0] … jobs[9] main
Exercise Explain the execution flow of the following program. public class Job extends Thread { public void run() {  // do some meaningful work, e.g.,  writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); jobs[i].join(); } } }
Thread Safety Example --- bank accounts public class Account { private long balance; public boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } // other methods and fields } What might happen in the presence of multiple threads?
Example (Cont.) Possible scenario Assume that the initial balance is $1,000,000, and two withdraw requests of $1,000,000 are made almost simultaneously.  time  balance  withdraw 1  withdraw 2 t1  1,000,000  amount<=balance  t2  1,000,000  amount<=balance  t3  1,000,000  newbalance=...;  t4  1,000,000  newbalance=...;  t5  0  balance=...;  t6  0  balance=...;  t7  0  return true;  t8  0  return true; if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; }
Atomicity of Operations The problem Several calls of the  withdraw method may be interleaved interfering with each others. Atomic operation An operation that can’t be interrupted, e.g., reading and assignment to (word-sized) variables. Non-atomic operations should be explicitly  synchronized  to ensure atomicity.
Making methods atomic Synchronized methods and statements A lock is associated with each object. // synchronized method synchronized  void aMethod() { // do something } // synchronized statement synchronized  (expr) { // expr should be of a reference type! // do somthing }
Example public class Account { private long balance; public  synchronized  boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } // other methods and fields }
Exercise Make withdraw method synchronized by using the synchronized statement. public class Account { private long balance; public boolean withdraw(long amount) { // WRITE YOUR CODE HERE… if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } }
Synchronization Synchronization Mechanism to controls the order in which threads execute Competition vs. cooperative synchronization Mutual exclusion of threads Each synchronized method or statement is guarded by an object.  When entering a synchronized method or statement, the object will be locked until the method is finished. When the object is locked by another thread, the current thread must wait.
Example --- Bounded Queue A first-in, first-out queue with a fixed capacity public class BoundedQueue { protected Object rep[]; // circular array protected int front = 0; // front of the queue protected int back = -1; // back of the queue protected int size; // capacity of the queue protected int count = 0; // num of objects in the queue //@ requires size > 0; public BoundedQueue(int size) { this.size = size; rep = new Object[size]; back = size – 1; } public boolean isEmpty() { return (count == 0); }
Bounded Queue (Cont.) public boolean isFull() { return (count == size); }  public int getCount() { return count; } public void put(/*@ non_null @*/ Object e) { if (!isFull()) { back = (back + 1) % size; rep[back] = e; count++; } } public Object get() { Object result = null; if (!isEmpty()) { result = rep[front]; front = (front + 1) % size; count--; } return result; }  }
Making Queue Thread-Safe public class SyncBoundedQueue extends BoundedQueue { public SyncBoundedQueue(int size) { super(size); } public  synchronized  boolean isEmpty() { return super.isEmpty(); } public  synchronized  boolean isFull() { return super.isFull(); } public  synchronized  int getCount() { return super.getCount(); } public  synchronized  void put(Object e) { super.put(e); } public  synchronized  Object get() { return super.get(); } }
Use of Bounded Queue Typical use in producers and consumers Thread Thief Broker SyncBoundedQueue
Producer public class Thief extends Thread { private BoundedQueue queue; private int n; // number of items to steal public Producer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { queue.put(new Integer(i)); System.out.println(“produced: “ + i); try { sleep((int) (Math.random() * 100)); } catch (InterruptedException e) {} } } }
Consumer public class Broker extends Thread { private BoundedQueue queue; private int n; // number of stolen items to sell public Consumer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { Object obj = queue.get(); System.out.println(“\tconsumed: “ + obj); try { sleep((int) (Math.random() * 400)); } catch (InterruptedException e) {} } } }
Sample Main Program The main method of class SyncBoundedQueue public static void main(String[] args) { SyncBoundedQueue queue = new SyncBoundedQueue(5); new Thief(queue, 15).start(); // produce 15 items new Broker(queue, 10).start(); // consume 10 items } Output Some of the items might be lost, as the producer produces items faster than the consumer consumes them.
Cooperation among Threads Guarded suspension Before a method is executed, the guard is tested. A  guard  is the precondition for an action to complete successfully. Execution continues only when the guard is true, ensuring the successful completion of the method invocation. Execution is temporarily suspended until the guard becomes true, at which time execution may continue. Thread controlling methods wait(), notify(), and notifyAll() defined in the class Object Can be used to implement guarded suspension
Thread Methods of Class Object Method    Description wait()   Temporarily blocked and placed in the wait queue   of the receiving object.  notify()   Awaken one of the threads in the wait queue    associated with the receiving object and remove it   from the queue. notifyAll() Same as notify() except that all threads in the wait    queue are awakened.
Bounded Queue with Guarded Suspension public class BoundedQueueWithGuard extends BoundedQueue { public Producer(int size) { super(size); } public synchronized boolean isEmpty() {  return super.isEmpty();  } public synchronized boolean isFull() {  return super.isFull();  } public synchronized int getCount() {  return super.getCount();  } <<put, get, and main method>> }
Put Method public synchronized void put(Object obj) { try { while (isFull()) { wait(); } } catch (InterruptedException e) {} super.put(e); notify(); }
Exercise --- Get Method Write the get method with guarded suspension. public synchronized Object get() { // WRITE YOUR CODE HERE. }
Smart Thief and Broker public static void main(String args[]) { BoundedQueueWithGuard queue = new BoundedQueueWithGuard(5); new Thief(queue, 15).start(); // produce 15 items. new Broker(queue, 15).start(); // consume 15 times } Output: No lost items!

More Related Content

ODP
Java Concurrency
PDF
Concurrency Concepts in Java
PDF
.NET Multithreading and File I/O
PDF
Java_practical_handbook
DOCX
Java practical
PDF
Advanced Java Practical File
PDF
Java Simple Programs
ODP
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java Concurrency
Concurrency Concepts in Java
.NET Multithreading and File I/O
Java_practical_handbook
Java practical
Advanced Java Practical File
Java Simple Programs
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning

What's hot (20)

ODP
Java Generics
PPTX
Effective java - concurrency
DOC
Final JAVA Practical of BCA SEM-5.
PDF
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
DOCX
Java PRACTICAL file
PPTX
classes & objects in cpp overview
PDF
Java Performance Puzzlers
PDF
JVM Mechanics
PPS
Class method
PDF
Java 7 at SoftShake 2011
PDF
Java 7 JUG Summer Camp
PDF
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
PDF
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
PDF
Java 7 LavaJUG
PPT
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
PDF
Actor Concurrency
PDF
Sam wd programs
PPT
Simple Java Programs
PDF
Java programs
Java Generics
Effective java - concurrency
Final JAVA Practical of BCA SEM-5.
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Java PRACTICAL file
classes & objects in cpp overview
Java Performance Puzzlers
JVM Mechanics
Class method
Java 7 at SoftShake 2011
Java 7 JUG Summer Camp
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coin
Java 7 LavaJUG
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
Actor Concurrency
Sam wd programs
Simple Java Programs
Java programs
Ad

Viewers also liked (9)

PPT
PPT
5.Dns Rpc Nfs
PPT
30 5 Database Jdbc
PPT
Jdbc
PPT
7.Canon & Dt
PPT
2.Public Vulnerability Databases
PDF
Ch06 Policy
PDF
7.Trust Management
PDF
Ch09 Information Security Best Practices
5.Dns Rpc Nfs
30 5 Database Jdbc
Jdbc
7.Canon & Dt
2.Public Vulnerability Databases
Ch06 Policy
7.Trust Management
Ch09 Information Security Best Practices
Ad

Similar to Thread (20)

PPT
Multithreading
PPT
Java Performance, Threading and Concurrent Data Structures
PPTX
Threads in Java
PDF
Java Thread Synchronization
PPTX
Concurrency
PPTX
Multi-threaded Programming in JAVA
PPT
Md09 multithreading
PPT
Developing Multithreaded Applications
PPTX
Chap3 multi threaded programming
PPTX
MSBTE Computer Engineering JPR java. multi. threading.pptx
PDF
Threads
PPTX
PPTX
Multithreading in java
PPTX
Multithreading in java
ODP
Multithreading Concepts
PPTX
Thread syncronization
PPT
Java Multithreading and Concurrency
PPTX
Threading in java - a pragmatic primer
PPTX
PPT
Threads in java
Multithreading
Java Performance, Threading and Concurrent Data Structures
Threads in Java
Java Thread Synchronization
Concurrency
Multi-threaded Programming in JAVA
Md09 multithreading
Developing Multithreaded Applications
Chap3 multi threaded programming
MSBTE Computer Engineering JPR java. multi. threading.pptx
Threads
Multithreading in java
Multithreading in java
Multithreading Concepts
Thread syncronization
Java Multithreading and Concurrency
Threading in java - a pragmatic primer
Threads in java

More from phanleson (20)

PDF
Learning spark ch01 - Introduction to Data Analysis with Spark
PPT
Firewall - Network Defense in Depth Firewalls
PPT
Mobile Security - Wireless hacking
PPT
Authentication in wireless - Security in Wireless Protocols
PPT
E-Commerce Security - Application attacks - Server Attacks
PPT
Hacking web applications
PPTX
HBase In Action - Chapter 04: HBase table design
PPT
HBase In Action - Chapter 10 - Operations
PPT
Hbase in action - Chapter 09: Deploying HBase
PPTX
Learning spark ch11 - Machine Learning with MLlib
PPTX
Learning spark ch10 - Spark Streaming
PPTX
Learning spark ch09 - Spark SQL
PPT
Learning spark ch07 - Running on a Cluster
PPTX
Learning spark ch06 - Advanced Spark Programming
PPTX
Learning spark ch05 - Loading and Saving Your Data
PPTX
Learning spark ch04 - Working with Key/Value Pairs
PPTX
Learning spark ch01 - Introduction to Data Analysis with Spark
PPT
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
PPT
Lecture 1 - Getting to know XML
PPTX
Lecture 4 - Adding XTHML for the Web
Learning spark ch01 - Introduction to Data Analysis with Spark
Firewall - Network Defense in Depth Firewalls
Mobile Security - Wireless hacking
Authentication in wireless - Security in Wireless Protocols
E-Commerce Security - Application attacks - Server Attacks
Hacking web applications
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 10 - Operations
Hbase in action - Chapter 09: Deploying HBase
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch10 - Spark Streaming
Learning spark ch09 - Spark SQL
Learning spark ch07 - Running on a Cluster
Learning spark ch06 - Advanced Spark Programming
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch01 - Introduction to Data Analysis with Spark
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Lecture 1 - Getting to know XML
Lecture 4 - Adding XTHML for the Web

Thread

  • 1. Concurrent Programming CS 3331 Fall 2007
  • 2. Outline What’re threads? How to create threads? How to controlling threads? Thread safety and synchronization BoundedQueue example
  • 3. Concurrent Programming Shared memory vs. message-based (e.g., IPC) Built-in language features vs. library-based
  • 4. Multithreaded Programming Thread A single sequential flow of control within a program, i.e., a lightweight flow that executes concurrently with other threads within a single process. Communicates with other threads via shared variables and objects. Managed by JVM. flow of control sequential concurrent
  • 5. Why? Can take advantages of multiple CPUs. Allows a server to handle multiple clients simultaneously. Suitable for developing reactive system. Makes application more responsive to user inputs (e.g., GUI programs such as BouncingBall applet). Natural to model real worlds.
  • 6. But, More difficult to program and debug (e.g., shared access to objects) Nondeterministic order of thread execution Safety and liveness problems
  • 7. Creating Threads in Java Two approaches By extending the java.lang.Thread class By implementing the java.lang.Runnable interface
  • 8. Creating Threads (Cont.) Extending the Thread class public class MyThread extends Thread { public void run() { // the thread body } // other methods and fields. } // Start a new thread by calling the start method MyThread thread = new MyThread(); thread.start(); // … flow of control start() run() // … The method run is the concurrent unit in Java.
  • 9. Creating Threads (Cont.) Implementing the Runnable interface public interface Runnable { void run(); } public class MyThread implements Runnable { public void run() { // the thread body } // other methods and fields. } // Start a new thread by creating an instance and calling the start method, e.g., new Thread(new MyThread()).start();
  • 10. Example --- Simple Counter public class Counter extends Thead { private int count, inc, delay; public Counter(int init, int inc, int delay) { this.count = init; this.inc = inc; this.delay = delay; } public void run() { try { for (;;) { System.out.print(count + “ “); count += inc; sleep(delay); } } catch (InterruptedException e) {} } public static void main(String[] args) { new Counter(0, 1, 33).start(); new Counter(0, -1, 100).start(); } }
  • 11. Example (Cont.) Output: 0 0 1 2 -1 3 4 5 -2 6 7 8 -3 9 10 -4 11 12 13 -5 14 15 16 -6 17 18 -7 19 20 21 -8 22 23 24 -9 25 26 -10 27 28 -11 29 30 31 -12 32 33 34 -13 35 36 37 -14 38 39 -15 40 41 42 -16 43 44 45 … Q: What will happen if we change the statement “new Counter().start()” to “new Counter().run()” in the main method?
  • 12. Exercise Write a thread class named Talker that prints a hello message, say “Hello, I am Tom!”, continuously, and create two instances as separate threads. public class Talker _______________________ { private String name; public Talker(String name) { this.name = name; } }
  • 13. Controlling Threads Life cycle of threads New After creation but before start() call Alive After the start() call; run() is implicitly called. Consists of two sub states Runnable : Ready to run; maybe running or waiting for their turns Non-runnable : Not ready to run; waiting for some events (e.g., sleeping, blocked, or waiting) Dead When run() method returns start Non-runnable New Dead Runnable yield sleep join end of run Alive
  • 14. Methods of Thread Class Method Description start() Enter the Runnable state from the new state *sleep() Enter the Non-runnable state join() Enter the Non-runnable state and wait for another thread to terminate *yield() Release CPU and remain in the Runnable interrupt() Cause InterruptedException isAlive() True if in the alive state isInterrupted() True if interrupted * static method
  • 15. Example Coordinating concurrent jobs public class Job extends Thread { public void run() { // do some meaningful work, e.g., writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); } for (int i = 0; i < jobs.length; i++) { jobs[i].join(); } } } jobs[0] … jobs[9] main
  • 16. Exercise Explain the execution flow of the following program. public class Job extends Thread { public void run() { // do some meaningful work, e.g., writing multithreaded applets. } public static void main(String[] args) { Job[] jobs = new Job[10]; for (int i = 0; i < jobs.length; i++) { jobs[i] = new Job(); jobs[i].start(); jobs[i].join(); } } }
  • 17. Thread Safety Example --- bank accounts public class Account { private long balance; public boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } // other methods and fields } What might happen in the presence of multiple threads?
  • 18. Example (Cont.) Possible scenario Assume that the initial balance is $1,000,000, and two withdraw requests of $1,000,000 are made almost simultaneously. time balance withdraw 1 withdraw 2 t1 1,000,000 amount<=balance t2 1,000,000 amount<=balance t3 1,000,000 newbalance=...; t4 1,000,000 newbalance=...; t5 0 balance=...; t6 0 balance=...; t7 0 return true; t8 0 return true; if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; }
  • 19. Atomicity of Operations The problem Several calls of the withdraw method may be interleaved interfering with each others. Atomic operation An operation that can’t be interrupted, e.g., reading and assignment to (word-sized) variables. Non-atomic operations should be explicitly synchronized to ensure atomicity.
  • 20. Making methods atomic Synchronized methods and statements A lock is associated with each object. // synchronized method synchronized void aMethod() { // do something } // synchronized statement synchronized (expr) { // expr should be of a reference type! // do somthing }
  • 21. Example public class Account { private long balance; public synchronized boolean withdraw(long amount) { if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } // other methods and fields }
  • 22. Exercise Make withdraw method synchronized by using the synchronized statement. public class Account { private long balance; public boolean withdraw(long amount) { // WRITE YOUR CODE HERE… if (amount <= balance) { long newBalance = balance – amount; balance = newBalance; return true; } else { return false; } } }
  • 23. Synchronization Synchronization Mechanism to controls the order in which threads execute Competition vs. cooperative synchronization Mutual exclusion of threads Each synchronized method or statement is guarded by an object. When entering a synchronized method or statement, the object will be locked until the method is finished. When the object is locked by another thread, the current thread must wait.
  • 24. Example --- Bounded Queue A first-in, first-out queue with a fixed capacity public class BoundedQueue { protected Object rep[]; // circular array protected int front = 0; // front of the queue protected int back = -1; // back of the queue protected int size; // capacity of the queue protected int count = 0; // num of objects in the queue //@ requires size > 0; public BoundedQueue(int size) { this.size = size; rep = new Object[size]; back = size – 1; } public boolean isEmpty() { return (count == 0); }
  • 25. Bounded Queue (Cont.) public boolean isFull() { return (count == size); } public int getCount() { return count; } public void put(/*@ non_null @*/ Object e) { if (!isFull()) { back = (back + 1) % size; rep[back] = e; count++; } } public Object get() { Object result = null; if (!isEmpty()) { result = rep[front]; front = (front + 1) % size; count--; } return result; } }
  • 26. Making Queue Thread-Safe public class SyncBoundedQueue extends BoundedQueue { public SyncBoundedQueue(int size) { super(size); } public synchronized boolean isEmpty() { return super.isEmpty(); } public synchronized boolean isFull() { return super.isFull(); } public synchronized int getCount() { return super.getCount(); } public synchronized void put(Object e) { super.put(e); } public synchronized Object get() { return super.get(); } }
  • 27. Use of Bounded Queue Typical use in producers and consumers Thread Thief Broker SyncBoundedQueue
  • 28. Producer public class Thief extends Thread { private BoundedQueue queue; private int n; // number of items to steal public Producer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { queue.put(new Integer(i)); System.out.println(“produced: “ + i); try { sleep((int) (Math.random() * 100)); } catch (InterruptedException e) {} } } }
  • 29. Consumer public class Broker extends Thread { private BoundedQueue queue; private int n; // number of stolen items to sell public Consumer(BoundedQueue queue, int n) { this.queue = queue; this.n = n; } public void run() { for (int i = 0; i < n; i++) { Object obj = queue.get(); System.out.println(“\tconsumed: “ + obj); try { sleep((int) (Math.random() * 400)); } catch (InterruptedException e) {} } } }
  • 30. Sample Main Program The main method of class SyncBoundedQueue public static void main(String[] args) { SyncBoundedQueue queue = new SyncBoundedQueue(5); new Thief(queue, 15).start(); // produce 15 items new Broker(queue, 10).start(); // consume 10 items } Output Some of the items might be lost, as the producer produces items faster than the consumer consumes them.
  • 31. Cooperation among Threads Guarded suspension Before a method is executed, the guard is tested. A guard is the precondition for an action to complete successfully. Execution continues only when the guard is true, ensuring the successful completion of the method invocation. Execution is temporarily suspended until the guard becomes true, at which time execution may continue. Thread controlling methods wait(), notify(), and notifyAll() defined in the class Object Can be used to implement guarded suspension
  • 32. Thread Methods of Class Object Method Description wait() Temporarily blocked and placed in the wait queue of the receiving object. notify() Awaken one of the threads in the wait queue associated with the receiving object and remove it from the queue. notifyAll() Same as notify() except that all threads in the wait queue are awakened.
  • 33. Bounded Queue with Guarded Suspension public class BoundedQueueWithGuard extends BoundedQueue { public Producer(int size) { super(size); } public synchronized boolean isEmpty() { return super.isEmpty(); } public synchronized boolean isFull() { return super.isFull(); } public synchronized int getCount() { return super.getCount(); } <<put, get, and main method>> }
  • 34. Put Method public synchronized void put(Object obj) { try { while (isFull()) { wait(); } } catch (InterruptedException e) {} super.put(e); notify(); }
  • 35. Exercise --- Get Method Write the get method with guarded suspension. public synchronized Object get() { // WRITE YOUR CODE HERE. }
  • 36. Smart Thief and Broker public static void main(String args[]) { BoundedQueueWithGuard queue = new BoundedQueueWithGuard(5); new Thief(queue, 15).start(); // produce 15 items. new Broker(queue, 15).start(); // consume 15 times } Output: No lost items!

Editor's Notes

  • #5: Squential : tuần tự Concurrently : đồng thời, kiêm
  • #6: Reactive : tác động trở lại, ảnh hưởng trở lại Suitable : phù hợp, thích hợp
  • #36: synchronized public Object get() { try { while (isEmpty()) { wait(); } } catch (InterruptedException e) {} Object result = super.get(); notify(); return result; }
  • #37: public synchronized Object get() { try { while (isEmpty()) { wait(); } } catch (InterruptedException e) {} Object result = super.get(); notify(); return result; }