}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; | |||||
} | } | ||||
} | } | ||||
@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()); | |||||
} | } | ||||
} | } |
}; | }; | ||||
for (Philosopher philosopher : philosophers) { | for (Philosopher philosopher : philosophers) { | ||||
Thread t = new Thread(philosopher); | Thread t = new Thread(philosopher); | ||||
t.start(); | t.start(); | ||||
} | } |
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){ | |||||
} | |||||
} |