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.
Wersja 2<br />
package prodcons2;<br />
public class ProducerConsumerTest2 {<br />
public static void main(String[] args) {<br />
CubbyHole2 c = new CubbyHole2();<br />
Producer1 p1 = new Producer1(c, 1);<br />
Consumer1 c1 = new Consumer1(c, 1);<br />
p1.start();<br />
c1.start();<br />
}<br />
}<br />
==============================================<br />
package prodcons2;<br />
public class Producer1 extends Thread {<br />
private CubbyHole2 cubbyhole;<br />
private int number;<br />
public Producer1(CubbyHole2 c, int number) {<br />
cubbyhole = c;<br />
this.number = number;<br />
}<br />
@Override<br />
public void run() {<br />
for (int i = 0; i < 10; i++) {<br />
cubbyhole.put(number, i);<br />
try {<br />
sleep((int)(Math.random() * 500));<br />
} catch (InterruptedException e) {<br />
System.err.println(e.getMessage());<br />
}<br />
}<br />
}<br />
}<br />
=================================================<br />
package prodcons2;<br />
public class Consumer1 extends Thread {<br />
private CubbyHole2 cubbyhole;<br />
private int number;<br />
public Consumer1(CubbyHole2 c, int number) {<br />
cubbyhole = c;<br />
this.number = number;<br />
}<br />
@Override<br />
public void run() {<br />
int value = 0;<br />
for (int i = 0; i < 10; i++) {<br />
value = cubbyhole.get(number);<br />
try {<br />
sleep((int)(Math.random() * 1000));<br />
} catch (InterruptedException e) {<br />
System.err.println(e.getMessage());<br />
}<br />
}<br />
}<br />
}<br />
=====================================================<br />
Lab. „<strong>Programowanie</strong> <strong>współbieżne</strong> w <strong>języku</strong> <strong>Java</strong>”<br />
10