Programowanie współbieżne w języku Java
Programowanie współbieżne w języku Java
Programowanie współbieżne w języku Java
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
package prodcons01;<br />
public class CubbyHole1 {<br />
private boolean available = false;<br />
private int contents = 0;<br />
public synchronized int get(int who) {<br />
while (available == false) {<br />
try {<br />
//Wait for Producer to put value.<br />
wait();<br />
} catch (InterruptedException e) { }<br />
}<br />
available = false;<br />
System.out.format("Consumer #%d got: %d%n", who, contents);<br />
//Notify Producer that value has been retrieved.<br />
notifyAll();<br />
return contents;<br />
}<br />
}<br />
public synchronized void put(int who, int value) {<br />
while (available == true) {<br />
try {<br />
//Wait for Consumer to get value.<br />
wait();<br />
} catch (InterruptedException e) { }<br />
}<br />
contents = value;<br />
available = true;<br />
System.out.format("Producer #%d put: %d%n", who, contents);<br />
//Notify Consumer that value has been set.<br />
notifyAll();<br />
}<br />
Lab. „<strong>Programowanie</strong> <strong>współbieżne</strong> w <strong>języku</strong> <strong>Java</strong>”<br />
9