Download presentation
Presentation is loading. Please wait.
Published byMagnus Hart Modified over 9 years ago
1
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
2
SJSU Dept. of Computer Science Spring 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
3
SJSU Dept. of Computer Science Spring 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 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. _
4
SJSU Dept. of Computer Science Spring 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
5
SJSU Dept. of Computer Science Spring 2014: April 30 CS 152: Programming Language Paradigms © R. Mak 5 Multithreaded Programming in Java Multithreaded programming involves having multiple 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. _
6
SJSU Dept. of Computer Science Spring 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.
7
SJSU Dept. of Computer Science Spring 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. }
8
SJSU Dept. of Computer Science Spring 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 } }
9
SJSU Dept. of Computer Science Spring 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
10
SJSU Dept. of Computer Science Spring 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!
11
SJSU Dept. of Computer Science Spring 2014: April 30 CS 152: Programming Language Paradigms © R. Mak 11 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.
12
SJSU Dept. of Computer Science Spring 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. _
13
SJSU Dept. of Computer Science Spring 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?
14
SJSU Dept. of Computer Science Spring 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();
15
SJSU Dept. of Computer Science Spring 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()) {... }
16
SJSU Dept. of Computer Science Spring 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.
17
SJSU Dept. of Computer Science Spring 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())); }
18
SJSU Dept. of Computer Science Spring 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())); }
19
SJSU Dept. of Computer Science Spring 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
20
SJSU Dept. of Computer Science Spring 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. _
21
SJSU Dept. of Computer Science Spring 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.
22
SJSU Dept. of Computer Science Spring 2014: April 30 CS 152: Programming Language Paradigms © R. Mak 22 "Message B" "Message A"
23
SJSU Dept. of Computer Science Spring 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! _
24
SJSU Dept. of Computer Science Spring 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
25
SJSU Dept. of Computer Science Spring 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--. ThreadOperationResult T1register1 = countregister1 = 5 T1register1 = register1 + 1register1 = 6 T2register2 = countregister2 = 5 T2register2 = register2 - 1register2 = 4 T1count = register1count = 6 T2count = register2count = 4
26
SJSU Dept. of Computer Science Spring 2014: April 30 CS 152: Programming Language Paradigms © R. Mak 26 Another Race Condition Example, cont’d 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. _
27
SJSU Dept. of Computer Science Spring 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. _
28
SJSU Dept. of Computer Science Spring 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 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. _
29
SJSU Dept. of Computer Science Spring 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.
30
SJSU Dept. of Computer Science Spring 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(); }
31
SJSU Dept. of Computer Science Spring 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);... }
32
SJSU Dept. of Computer Science Spring 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);... }
33
SJSU Dept. of Computer Science Spring 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);... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.