CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose...

17
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

Transcript of CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose...

Page 1: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

CS 151: Object-Oriented Design November 21 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2013Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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

Page 3: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 2013: November 21

CS 151: Object-Oriented Design© R. Mak

3

Thread States

Each thread has a stateand a priority.

A state can be blockedif it is: sleeping waiting for I/O waiting to acquire a lock waiting for a condition

No state for athread that’s

running.

Page 4: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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?

Page 5: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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()) { ... }

Page 6: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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.

_

Page 7: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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()));}

Page 8: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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()));}

Page 9: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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

Page 10: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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._

Page 11: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 2013: November 21

CS 151: Object-Oriented Design© R. Mak

11

Quiz2013Nov21

Page 12: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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.

Page 13: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 2013: November 21

CS 151: Object-Oriented Design© R. Mak

13

"Message B"

"Message B"

"Message B"

"Message A"

Page 14: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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!

_

Page 15: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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--.

Page 16: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 2013: November 21

CS 151: Object-Oriented Design© R. Mak

16

Another Race Condition Example

Thread Operation Result

T1 register1 = count register1 = 5

T1 register1 = register1 + 1 register1 = 6

T2 register2 = count register2 = 5

T2 register2 = register2 - 1 register2 = 4

T1 count = register1 count = 6

T2 count = register2 count = 4

Whether count ends up equal to 6 or 4 depends on the order of the last two instructions._

Page 17: CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak.

SJSU Dept. of Computer ScienceFall 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 thatoperates 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.