Top Banner
CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu /~mak
33

CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Jan 06, 2018

Download

Documents

Magnus Hart

SJSU Dept. of Computer Science Spring 2014: April 30 CS 152: Programming Language Paradigms © R. Mak 3 CS Department Colloquium, cont’d  See  Up to 10 extra credit points if you attend. Added to your final score.  You must write a short summary of what the talk was about. One or two pages. Due Monday, May 5. _
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

CS 152: Programming Language Paradigms

April 30 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

2

CS Department Colloquium Prof. Juliette Kennedy

Professor of Mathematics University of Helsinki

Talk: “Reading Gödel’s 1946 Princeton Bicentennial Lecture” About the problem of transferring the

Turing analysis of computability to the cases of definability and provability.

Thursday, May 1, 3:00 PM MacQuarrie Hall, room 225

Page 3: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

3

CS Department Colloquium, cont’d

See http://www.cs.sjsu.edu/sp14/kennedy.html

Up to 10 extra credit points if you attend. Added to your final score.

You must write a short summary of what the talk was about. One or two pages. Due Monday, May 5.

_

Page 4: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

4

Computer History Museum

Saturday, May 3 at 11:30 AM.

Revolution Exhibit

IBM 1401 Demo Lab at 11:45 and 12:30 Not everybody at once, please!

Babbage Difference Engine Demo at 1:00

Extra-credit quiz To be posted to Canvas and Piazza

Page 5: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

5

Multithreaded Programming in Java

Multithreaded programming involves havingmultiple threads of execution happening concurrently within a single Java program.

A thread is a program unit that executes independently of other parts of the program._

Page 6: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

6

Thread vs. Process

A process is an executing program managed by the operating system.

A process has its own resources, such as its separate runtime stack and its separate share of main memory.

Switching between processes is relatively slow.

A thread runs within a single process.

Process resources such as memory are shared. Thread switching is very fast.

Page 7: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

7

How to Create a Java Thread

Define a class that implements the Runnable interface:

Put the code that you want to run in parallel with other code in the run() method.

public interface Runnable{ void run();}

public class GreetingProducer implements Runnable { ...

public void run() { // Code to run in parallel here. }}

Page 8: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

8

Runnable Example

Repeatedly print a greeting. Thread.sleep() yields control to another thread.

Can throw the InterruptedException exception.

public void run() { try { for (int i = 1; i <= REPETITIONS; i++) { System.out.println(i + ": " + greeting); Thread.sleep(DELAY); } } catch (InterruptedException exception) { // do nothing }}

Page 9: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

9

Runnable Example, cont’d Example:

Create two threads. Each thread loops to repeatedly print a greeting.

Create a Runnable object of the class. Construct a Thread object using the Runnable object. Call the start() method of the Thread object.

Runnable r1 = new GreetingProducer("Hello, World!");Runnable r2 = new GreetingProducer("Goodbye, World!");

Thread t1 = new Thread(r1);Thread t2 = new Thread(r2);

t1.start();t2.start();

Demo: threads

Page 10: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

10

Runnable Example, cont’d

The output may not always be perfectly interleaved! The thread scheduler

does not guarantee the order that the threads will execute.

1: Hello, World! 1: Goodbye, World! 2: Hello, World! 2: Goodbye, World! 3: Hello, World! 3: Goodbye, World! 4: Hello, World! 4: Goodbye, World! 5: Hello, World! 5: Goodbye, World! 6: Hello, World! 6: Goodbye, World! 7: Hello, World! 7: Goodbye, World! 8: Goodbye, World! 8: Hello, World! 9: Goodbye, World! 9: Hello, World! 10: Goodbye, World! 10: Hello, World!

Page 11: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

11

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’srunning.

Page 12: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

12

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._

Page 13: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

13

Choosing Threads to Run

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 14: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

14

Terminating Threads

A thread terminates “naturally” when its run() method completes.

You can also interrupt a running thread from another thread:_ t1.interrupt();

Page 15: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

15

Terminating Threads, cont’d

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._

if (Thread.currentThread().isInterrupted()) { ... }

Page 16: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

16

Thread Synchronization Example

Suppose a bounded queue object (circular buffer) 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 17: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

17

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 18: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

18

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 19: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

19

Thread Synchronization, cont’d Create the queue. 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 20: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

20

What Can Go Wrong?

The consumer can’t retrieve all the messages that the producers inserted.

The consumer retrieves the same message multiple times._

Page 21: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

21

What Can Go Wrong? 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 22: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

22

"Message B"

"Message B"

"Message B"

"Message A"

Page 23: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

23

A Race Condition

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 24: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

24

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

Page 25: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

25

Another Race Condition Example, cont’d

Suppose the value of count is 5 and that thread T1 executes count++ at the same time that thread T2 executes count--.

Thread Operation ResultT1 register1 = count register1 = 5T1 register1 = register1 + 1 register1 = 6T2 register2 = count register2 = 5T2 register2 = register2 - 1 register2 = 4T1 count = register1 count = 6T2 count = register2 count = 4

Page 26: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

26

Another Race Condition Example, cont’d

Thread Operation ResultT1 register1 = count register1 = 5T1 register1 = register1 + 1 register1 = 6T2 register2 = count register2 = 5T2 register2 = register2 - 1 register2 = 4T1 count = register1 count = 6T2 count = register2 count = 4

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

Page 27: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

27

Mutual Exclusion

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._

Page 28: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

28

Critical Region

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._

Page 29: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

29

A Solution: Locks

When one thread enters its critical region, another thread must be prevented from entering its critical region.

The first thread must set a lock before entering the critical region, execute the code in the critical region, and then unlock the lock.

If another thread attempts to enter its critical region, that thread must wait until the lock is unlocked.

Page 30: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

30

Locks, cont’d

Java provides re-entrant locks.

But is it sufficient to lock up the add() and remove() code of BoundedQueue?

Demo: queue2

Lock aLock = new ReentrantLock();...aLock.lock();try { critical region}finally { aLock.unlock();}

Page 31: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

31

Locks, cont’d

Consider what the Producer does:if (!queue.isFull()) { Message msg = new Message(index, i, text); queue.add(msg); ...}

Page 32: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

32

Locks, cont’d

Suppose the first Producer thread determines that the queue is not full.

But before it can call queue.add(), another Producer thread fills up the queue.

When the first Producer thread is reactivated, it adds to a full queue and thereby corrupts it.

if (!queue.isFull()) { Message msg = new Message(index, i, text); queue.add(msg); ...}

Page 33: CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

SJSU Dept. of Computer ScienceSpring 2014: April 30

CS 152: Programming Language Paradigms© R. Mak

33

Locks, cont’d

Therefore, testing whether the queue is full and adding to the queue must happen together under the same lock._

if (!queue.isFull()) { Message msg = new Message(index, i, text); queue.add(msg); ...}