Parallel adding kinda works
This commit is contained in:
parent
b67396d350
commit
eadcc6fe1c
@ -62,4 +62,13 @@ public class Schlenglein <T>{
|
||||
System.arraycopy(temp, 0, myArray, 0, maxSize / 2);
|
||||
}
|
||||
}
|
||||
|
||||
public void printAll(){
|
||||
System.out.print("[");
|
||||
for (int i = 0; i < this.size()-1; i++) {
|
||||
System.out.print(this.myArray[i] + ", ");
|
||||
}
|
||||
System.out.println(this.myArray[this.size()-1] + "]");
|
||||
|
||||
}
|
||||
}
|
||||
|
19
src/part4/aufg2/Demo.java
Normal file
19
src/part4/aufg2/Demo.java
Normal file
@ -0,0 +1,19 @@
|
||||
package part4.aufg2;
|
||||
|
||||
/*
|
||||
In einem System arbeiten eine Reihe von Benutzer, die von Zeit zu Zeit
|
||||
Druckaufträge eines gewissen Umfangs erzeugen.
|
||||
Diese werden in einer Druckerwarteschlange verwaltet.
|
||||
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.
|
||||
*/
|
||||
|
||||
public class Demo {
|
||||
public static void main(String[] args) {
|
||||
User u1 = new User(1);
|
||||
User u2 = new User(2);
|
||||
u1.start();
|
||||
u2.start();
|
||||
}
|
||||
}
|
18
src/part4/aufg2/Printer.java
Normal file
18
src/part4/aufg2/Printer.java
Normal file
@ -0,0 +1,18 @@
|
||||
package part4.aufg2;
|
||||
|
||||
import part3.aufg2.Schlenglein;
|
||||
|
||||
public class Printer {
|
||||
static Schlenglein q;
|
||||
|
||||
public void run(){
|
||||
synchronized (getClass()){
|
||||
try {
|
||||
q.wait();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
40
src/part4/aufg2/User.java
Normal file
40
src/part4/aufg2/User.java
Normal file
@ -0,0 +1,40 @@
|
||||
package part4.aufg2;
|
||||
|
||||
import part3.aufg2.Schlenglein;
|
||||
|
||||
|
||||
|
||||
public class User <T> extends Thread {
|
||||
private int threadNumber;
|
||||
static Schlenglein q = new Schlenglein<String>();
|
||||
|
||||
public User(int threadNumber) {
|
||||
this.threadNumber = threadNumber;
|
||||
}
|
||||
|
||||
public synchronized void makeRequest() {
|
||||
synchronized (getClass()) {
|
||||
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
|
||||
String randomIntStr = this.threadNumber + " " + randomInt;
|
||||
q.add(randomIntStr);
|
||||
q.printAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
// q.notify();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
for (int i = 0; i<10; i++) {
|
||||
makeRequest();
|
||||
try {
|
||||
sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user