Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: 00000000101010100101010111111111 Which.

Similar presentations


Presentation on theme: "Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: 00000000101010100101010111111111 Which."— Presentation transcript:

1 Concurrency (3) CSE 132

2 iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: 00000000101010100101010111111111 Which order below represents lowest to highest? A: 00000000 01010101 10101010 11111111 B: 10101010 11111111 00000000 01010101 C: 11111111 10101010 00000000 01010101 D: 11111111 00000000 01010101 10101010 E: none of the above

3 Synchronization Review Implicit “lock” associated with every object Synchronized methods acquire object’s lock prior to entering method and then release lock once method is complete Synchronized statement allows finer control (illustrated next) Either way, lock enforces mutual exclusion, only one thread at a time is allowed in “critical section,” or region of code controlled by lock

4 Synchronized statement public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); } Argument is object whose lock is acquired Doesn’t synchronize add() method invocation Statements within synchronized block are “atomic”

5 Separating acquire and release Sync interface Mutex class Three methods: – void acquire() Wait until lock is available – boolean attempt(long msec) Wait until lock is available or until msec time passes – Void release() Release lock acquire() and release() need not be nearby to one another

6 Reader/Writer Locks Allow for more than one thread to have lock at the same time Only safe for read-only access Exclusive write – concurrent read Enforce only one of these statements to be true: – There are currently no readers or writers. – There is currently a positive number of readers but no writer. – There is currently exactly one writer but no readers.

7 Guards Wait until some other thread sets a value public void guardedJoy() { // Simple loop guard. Wastes // processor time. Don't do this! while(!joy) {} System.out.println("Joy has been achieved!"); }

8 Wait method public synchronized guardedJoy() { // This guard only loops once for each special event, // which may not be the event we're waiting for. while(!joy) { try { wait(); } catch (InterruptedException e) {} } System.out.println("Joy and efficiency have been achieved!"); } wait() releases control until an “event” occurs

9 Use of wait() Must own lock on object before invoking wait() NOTE: guardedJoy() is synchronized! When wait() is invoked, lock is released and thread is suspended (i.e., not executing), waiting for events Upon event notification, thread is awakened and wait() returns (with the lock)

10 Waking up Sleeping Threads Method notifyAll() public synchronized notifyJoy() { joy = true; notifyAll(); } Must own lock to invoke notifyAll() It knows nothing about joy! Put wait() in loop!

11 iClicker/WUTexter Question Which of the following statements is true? Note: One is true, and the rest are false A:An unchecked exception must have a try…catch block B:The.start() method initiates threaded execution C:The.run() method initiates threaded execution D:A method declared synchronized may specify the object to be locked E:A synchronized block may specify a separate method to release a lock

12 Quiz 1 In lab Wednesday. Link to Quiz 1 (Google form) will be provided on class web page. Password will be provided in lab. Complete (by yourself!) before you leave lab on Wednesday.


Download ppt "Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: 00000000101010100101010111111111 Which."

Similar presentations


Ads by Google