Compare commits
3 Commits
b3bc487347
...
b5d79e2b57
Author | SHA1 | Date | |
---|---|---|---|
|
b5d79e2b57 | ||
|
eadcc6fe1c | ||
|
b67396d350 |
@ -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
|
||||||
@ -62,4 +66,13 @@ public class Schlenglein <T>{
|
|||||||
System.arraycopy(temp, 0, myArray, 0, maxSize / 2);
|
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] + "]");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
12
src/part4/aufg1/Demo.java
Normal file
12
src/part4/aufg1/Demo.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package part4.aufg1;
|
||||||
|
|
||||||
|
public class Demo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ThreadedCounter tc = new ThreadedCounter(-1,1);
|
||||||
|
ThreadedCounter tC = new ThreadedCounter(4,2);
|
||||||
|
|
||||||
|
tc.start();
|
||||||
|
tC.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
30
src/part4/aufg1/ThreadedCounter.java
Normal file
30
src/part4/aufg1/ThreadedCounter.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package part4.aufg1;
|
||||||
|
|
||||||
|
public class ThreadedCounter extends Thread {
|
||||||
|
private final int threadNumber;
|
||||||
|
private final int increment;
|
||||||
|
static int sharedInt = 0;
|
||||||
|
static int iterations = 0;
|
||||||
|
|
||||||
|
public ThreadedCounter(int increment, int threadNumber) {
|
||||||
|
this.increment = increment;
|
||||||
|
this.threadNumber = threadNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (sharedInt < 1000){
|
||||||
|
synchronized (getClass()) {
|
||||||
|
iterations++;
|
||||||
|
sharedInt += increment;
|
||||||
|
System.out.println("Thread " + threadNumber + " made the shared Int: " + sharedInt);
|
||||||
|
|
||||||
|
try {
|
||||||
|
sleep(10);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("it took " + iterations + " iterations");
|
||||||
|
}
|
||||||
|
}
|
28
src/part4/aufg2/Demo.java
Normal file
28
src/part4/aufg2/Demo.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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) {
|
||||||
|
int numUsers = 7;
|
||||||
|
int numPrinter = 4;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
src/part4/aufg2/Printer.java
Normal file
31
src/part4/aufg2/Printer.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package part4.aufg2;
|
||||||
|
|
||||||
|
public class Printer extends Thread {
|
||||||
|
private int threadNumber;
|
||||||
|
|
||||||
|
public Printer(int threadNumber){
|
||||||
|
this.threadNumber = threadNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
synchronized (Puwwer.q) {
|
||||||
|
if (Puwwer.q.isEmpty()){
|
||||||
|
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();
|
||||||
|
}
|
36
src/part4/aufg2/User.java
Normal file
36
src/part4/aufg2/User.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package part4.aufg2;
|
||||||
|
|
||||||
|
public class User extends Thread {
|
||||||
|
private int threadNumber;
|
||||||
|
public User(int threadNumber) {
|
||||||
|
this.threadNumber = threadNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void makeRequest() {
|
||||||
|
synchronized (Puwwer.q) {
|
||||||
|
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
|
||||||
|
String randomIntStr = this.threadNumber + " " + randomInt;
|
||||||
|
|
||||||
|
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() {
|
||||||
|
while (true) {
|
||||||
|
makeRequest();
|
||||||
|
try {
|
||||||
|
sleep(3000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user