Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 2 CS Department Colloquium  Dr. Richard Stallman President, Free Software Foundation Developer of the GNU operating system MacArthur Foundation fellowship  Talk: “For a Free Digital Society” About the many threats to freedom in the digital society.  Thursday, November 21, 4:30 PM  Washington Square Hall Room 109

3 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 3 Thread States  Each thread has a state and a priority.  A state can be blocked if it is: sleeping waiting for I/O waiting to acquire a lock waiting for a condition No state for a thread that’s running.

4 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 4 Activating Threads  The thread scheduler activates a thread whenever: a running thread has completed its time slice a running thread blocks itself a thread with a higher priority becomes available  The thread scheduler determines which thread to run: It selects the highest priority thread that is currently runnable. The Java standard does not specify which thread among the eligible ones should be scheduled.  random selection?  round robin?

5 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 5 Terminating Threads  A thread terminates “naturally” when its run() method completes.  You can also interrupt a running thread from another thread: The thread can test whether the interrupt flag has been set: Or it can catch the InterruptedException thrown by the Thread.sleep() method when the thread is interrupted.  Terminate the run() method after an interruption. t1.interrupt(); if (Thread.currentThread().isInterrupted()) {... }

6 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 6 Thread Synchronization Example  Recall the bounded queue object (circular buffer).  Suppose it is shared among several threads.  Two producer threads add messages. One adds “A Message” The other adds “B Message” Each prints the messages as it adds them.  One consumer thread removes messages. Prints the messages as it removes them. _

7 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 7 Thread Synchronization, cont’d  The run() method of the Producer class: int i = 1; while (i <= count) { if (!queue.isFull()) { Message msg = new Message(index, i, text); queue.add(msg);... i++; } Thread.sleep((int) (DELAY*Math.random())); }

8 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 8 Thread Synchronization, cont’d  The run() method of the Consumer class: int i = 1; while (i <= count) { if (!queue.isEmpty()) { Message msg = queue.remove();... i++; } Thread.sleep((int) (DELAY*Math.random())); }

9 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 9 Thread Synchronization, cont’d  Create the queue, and create and start the threads: BoundedQueue queue = new BoundedQueue(10); final int PRODUCER_C0UNT = 2; final int MESSAGE_COUNT = 100; Runnable run1 = new Producer("A Message", queue, 1, MESSAGE_COUNT); Runnable run2 = new Producer("B Message", queue, 2, MESSAGE_COUNT); Runnable run3 = new Consumer(queue, PRODUCER_C0UNT*MESSAGE_COUNT); Thread thread1 = new Thread(run1); Thread thread2 = new Thread(run2); Thread thread3 = new Thread(run3); thread1.start(); thread2.start(); thread3.start(); Demo: queue1

10 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 10 A Race Condition  What can go wrong with the program? The consumer can’t retrieve all the messages that the producers inserted. The consumer retrieves the same message multiple times. _

11 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 11 Quiz 2013Nov21

12 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 12 A Race Condition, cont’d  How can such problems occur? 1. The first producer thread calls add() and executes elements[tail] = newValue; 2. The first producer thread reaches the end of its time slice. 3. The second producer thread calls add() and executes elements[tail] = newValue; tail++; 4. The second producer thread reaches the end of its time slice. 5. The first producer thread executes tail++;  Bad consequences! Step 1 starts to add a message to the queue. Step 3 overwrites the message added in step 1. Step 5 increments the tail index without filling the location, leaving behind garbage.

13 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 13 "Message B" "Message A"

14 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 14 A Race Condition, cont’d  A race condition occurs when multiple threads access and modify some shared data, and the final result depends on the order that the threads modified the data. An extremely bad situation. Results are often unpredictable and unrepeatable. Extremely hard to debug. Must avoid! _

15 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 15 Another Race Condition Example  Suppose variable count is shared.  count++ can be implemented as: register1 = count register1 = register1 + 1 count = register1  count-- can be implemented as: register2 = count register2 = register2 – 1 count = register2  Suppose the value of count is 5 and that thread T1 executes count++ at the same time that thread T2 executes count--.

16 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 16 Another Race Condition Example ThreadOperationResult T1register1 = countregister1 = 5 T1register1 = register1 + 1register1 = 6 T2register2 = countregister2 = 5 T2register2 = register2 - 1register2 = 4 T1count = register1count = 6 T2count = register2count = 4  Whether count ends up equal to 6 or 4 depends on the order of the last two instructions. _

17 SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 17 Critical Region  One way to avoid race conditions is to prevent more than one thread from reading and writing the shared data at the same time. This is called mutual exclusion. Thread schedulers provide different primitives to achieve mutual exclusion.  A critical region (AKA critical section) is the part of a program’s code that operates on some shared data. We need to prevent two threads from accessing that shared data at the same time. Therefore, we need to prevent two threads from being in their critical regions at the same time.


Download ppt "CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google