Parallel works as long as numUsers >= numPrinters
This commit is contained in:
parent
eadcc6fe1c
commit
b5d79e2b57
@ -38,6 +38,10 @@ public class Schlenglein <T>{
|
|||||||
return this.maxSize;
|
return this.maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty(){
|
||||||
|
return this.size()<=0;
|
||||||
|
}
|
||||||
|
|
||||||
public void attemptDynamicResize(){
|
public void attemptDynamicResize(){
|
||||||
if (numberOfElements < 0.2*maxSize){
|
if (numberOfElements < 0.2*maxSize){
|
||||||
// Kleiner Machen
|
// Kleiner Machen
|
||||||
|
@ -9,11 +9,20 @@ Zwei Drucker stehen zur Verfügung und arbeiten die Aufträge ab.
|
|||||||
Modellieren Sie das Problem als Erzeuger‐Verbraucher‐Problem und realisieren Sie es in Java.
|
Modellieren Sie das Problem als Erzeuger‐Verbraucher‐Problem und realisieren Sie es in Java.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public class Demo {
|
public class Demo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
User u1 = new User(1);
|
int numUsers = 7;
|
||||||
User u2 = new User(2);
|
int numPrinter = 4;
|
||||||
u1.start();
|
|
||||||
u2.start();
|
for (int i = 1; i <= numUsers; i++) {
|
||||||
|
User user = new User(i);
|
||||||
|
user.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= numPrinter ; i++) {
|
||||||
|
Printer printer = new Printer(i);
|
||||||
|
printer.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,31 @@
|
|||||||
package part4.aufg2;
|
package part4.aufg2;
|
||||||
|
|
||||||
import part3.aufg2.Schlenglein;
|
public class Printer extends Thread {
|
||||||
|
private int threadNumber;
|
||||||
|
|
||||||
public class Printer {
|
public Printer(int threadNumber){
|
||||||
static Schlenglein q;
|
this.threadNumber = threadNumber;
|
||||||
|
}
|
||||||
|
|
||||||
public void run(){
|
public void run() {
|
||||||
synchronized (getClass()){
|
while (true) {
|
||||||
try {
|
try {
|
||||||
q.wait();
|
synchronized (Puwwer.q) {
|
||||||
} catch (InterruptedException e) {
|
if (Puwwer.q.isEmpty()){
|
||||||
throw new RuntimeException(e);
|
try {
|
||||||
|
Puwwer.q.wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Printer " + this.threadNumber + " prints " + Puwwer.q.get());
|
||||||
|
}
|
||||||
|
} catch (NullPointerException e){}
|
||||||
|
try {
|
||||||
|
sleep(100);
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
src/part4/aufg2/Puwwer.java
Normal file
7
src/part4/aufg2/Puwwer.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package part4.aufg2;
|
||||||
|
|
||||||
|
import part3.aufg2.Schlenglein;
|
||||||
|
|
||||||
|
public class Puwwer {
|
||||||
|
static Schlenglein q = new Schlenglein();
|
||||||
|
}
|
@ -1,40 +1,36 @@
|
|||||||
package part4.aufg2;
|
package part4.aufg2;
|
||||||
|
|
||||||
import part3.aufg2.Schlenglein;
|
public class User extends Thread {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class User <T> extends Thread {
|
|
||||||
private int threadNumber;
|
private int threadNumber;
|
||||||
static Schlenglein q = new Schlenglein<String>();
|
|
||||||
|
|
||||||
public User(int threadNumber) {
|
public User(int threadNumber) {
|
||||||
this.threadNumber = threadNumber;
|
this.threadNumber = threadNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void makeRequest() {
|
public synchronized void makeRequest() {
|
||||||
synchronized (getClass()) {
|
synchronized (Puwwer.q) {
|
||||||
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
|
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
|
||||||
String randomIntStr = this.threadNumber + " " + randomInt;
|
String randomIntStr = this.threadNumber + " " + randomInt;
|
||||||
q.add(randomIntStr);
|
|
||||||
q.printAll();
|
Puwwer.q.add(randomIntStr);
|
||||||
|
|
||||||
|
System.out.println("User " + threadNumber + " added " + randomIntStr);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Puwwer.q.notify();
|
||||||
|
} catch (IllegalMonitorStateException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
while (true) {
|
||||||
try {
|
|
||||||
// q.notify();
|
|
||||||
} catch (IllegalMonitorStateException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
for (int i = 0; i<10; i++) {
|
|
||||||
makeRequest();
|
makeRequest();
|
||||||
try {
|
try {
|
||||||
sleep(100);
|
sleep(3000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user