Änderungen 05.12.2023 um 14:44

This commit is contained in:
leonmcfly 2023-12-05 16:09:12 +01:00
parent 582ed1a694
commit f6668dfce7
4 changed files with 62 additions and 1 deletions

View File

@ -24,7 +24,7 @@ public class Fork {
}catch (InterruptedException e){ }catch (InterruptedException e){
System.out.println("Gabel mit ID: " + id + " hat nicht funktioniert"); System.out.println("Gabel mit ID: " + id + " hat nicht funktioniert");
} }
return true return true;
} }
} }

View File

@ -13,6 +13,40 @@ public class Philosopher implements Runnable{
@Override @Override
public void run(){ public void run(){
while(true) {
allocateForks();
System.out.println("Philosopher " + id + " is eating");
try{
Thread.sleep(2000);
}catch (Exception e){
}
left.put();
right.put();
System.out.println("Philosopher " + id + " finished eating");
}
}
protected void allocateForks() {
Fork firstFork, secondFork;
if (left.getId() < right.getId()){
firstFork = left;
secondFork = right;
} else {
firstFork = right;
secondFork = left;
}
System.out.println("Philosopher " + id + " is trying to take Fork " + firstFork.getId());
while (!firstFork.take())
Thread.yield();
System.out.println("Philosopher " + id + " got Fork " + firstFork.getId());
System.out.println("Philosopher " + id + " is trying to take Fork " + secondFork.getId());
while (!secondFork.take())
Thread.yield();
System.out.println("Philosopher " + id + " got Fork " + secondFork.getId());
} }
} }

View File

@ -15,6 +15,7 @@ public class Table {
}; };
for (Philosopher philosopher : philosophers) { for (Philosopher philosopher : philosophers) {
Thread t = new Thread(philosopher); Thread t = new Thread(philosopher);
t.start(); t.start();
} }

View File

@ -0,0 +1,26 @@
package Prog3.src.DistributedSorter;
import java.util.Scanner;
public class SorterClient {
public static void main(String[] args) {
SorterClient client = new SorterClient();
client.run();
}
public void run(){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.isEmpty())
break;
line = sort(line);
System.out.println(line);
}
scanner.close();
}
private String sort(String line){
}
}