concurrency - eecs 4315 · 2018-03-07 · wait and notify the object class contains the following...
TRANSCRIPT
![Page 2: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/2.jpg)
Java code
public static void main(String[] args) {
Printer one = new Printer("1");
one.run();
}
2/16
![Page 3: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/3.jpg)
Executions
Question
Draw the state-transition diagram.
3/16
![Page 4: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/4.jpg)
Executions
4/16
![Page 5: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/5.jpg)
Java code
public static void main(String[] args) {
Printer one = new Printer("1");
Printer two = new Printer("2");
one.start();
two.start();
}
5/16
![Page 6: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/6.jpg)
Executions
Question
Draw the state-transition diagram.
6/16
![Page 7: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/7.jpg)
Executions
7/16
![Page 8: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/8.jpg)
Counter class
Problem
Implement the class Counter with attribute value, initialized tozero, and the methods increment and decrement.
Question
Can multiple threads share a Counter object and use methodssuch as increment and decrement concurrently?
Answer
No, as before, if two threads invoke increment concurrently, thecounter may only be incremented by one (rather than two).
8/16
![Page 9: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/9.jpg)
Counter class
Problem
Implement the class Counter with attribute value, initialized tozero, and the methods increment and decrement.
Question
Can multiple threads share a Counter object and use methodssuch as increment and decrement concurrently?
Answer
No, as before, if two threads invoke increment concurrently, thecounter may only be incremented by one (rather than two).
8/16
![Page 10: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/10.jpg)
Counter class
Problem
Implement the class Counter with attribute value, initialized tozero, and the methods increment and decrement.
Question
Can multiple threads share a Counter object and use methodssuch as increment and decrement concurrently?
Answer
No, as before, if two threads invoke increment concurrently, thecounter may only be incremented by one (rather than two).
8/16
![Page 11: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/11.jpg)
Synchronized methods
Methods such as increment should be executed atomically. Thiscan be accomplished by declaring the method to besynchronized.
A lock is associated with every object. For threads to execute asynchronized method on such the object, first its lock needs tobe acquired.
public synchronized void increment() {
this.value++;
}
9/16
![Page 12: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/12.jpg)
Synchronized methods
Methods such as increment should be executed atomically. Thiscan be accomplished by declaring the method to besynchronized.
A lock is associated with every object. For threads to execute asynchronized method on such the object, first its lock needs tobe acquired.
public synchronized void increment() {
this.value++;
}
9/16
![Page 13: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/13.jpg)
Resource class
Problem
Implement the class Resource with attribute available,initialized to true, and the methods acquire and release.
10/16
![Page 14: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/14.jpg)
Wait and notify
The Object class contains the following three methods:
wait: causes the current thread to wait for this object’s lockuntil another thread wakes it up.
notify: wakes up a single thread waiting on this object’slock; if there is more than one waiting, an arbitrary one ischosen; if there are none, nothing is done.
notifyAll: wakes up all threads waiting on this objects lock.
Since every class extends the class Object, these methods areavailable to every object.
11/16
![Page 15: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/15.jpg)
Wait and notify
The Object class contains the following three methods:
wait: causes the current thread to wait for this object’s lockuntil another thread wakes it up.
notify: wakes up a single thread waiting on this object’slock; if there is more than one waiting, an arbitrary one ischosen; if there are none, nothing is done.
notifyAll: wakes up all threads waiting on this objects lock.
Since every class extends the class Object, these methods areavailable to every object.
11/16
![Page 16: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/16.jpg)
States of a thread
runnable scheduler running
blocked
waitnotify
12/16
![Page 17: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/17.jpg)
User class
public class User extends Thread {
private Resource resource;
public User(Resource resource) {
super();
this.resource = resource;
}
public void run() {
super.run();
this.resource.acquire();
this.resource.release();
}
}
13/16
![Page 18: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/18.jpg)
Main method
final Resource resource = new Resource();
final int USERS = 2;
final User[] users = new User[USERS];
for (int i = 0; i < USERS; i++) {
users[i] = new User(resource);
}
for (int i = 0; i < USERS; i++) {
users[i].start();
}
14/16
![Page 19: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/19.jpg)
Configuration file
target=Main
classpath=<folder that contains Main.class>
listener=listeners.StateSpaceWithThreadInfo
native_classpath=<folder that contains
listener/StateSpaceWithThreadInfo.class>
15/16
![Page 20: Concurrency - EECS 4315 · 2018-03-07 · Wait and notify The Object class contains the following three methods: wait: causes the current thread to wait for this object’s lock until](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea1f7b537f0cc0410240349/html5/thumbnails/20.jpg)
State space
16/16