import java.util.concurrent.locks.Lock; | |||||
import java.util.concurrent.locks.ReentrantLock; | |||||
public class DiningPhilosophers { | |||||
// Anzahl der Philosophen | |||||
private static final int NUM_PHILOSOPHERS = 5; | |||||
public static void main(String[] args) { | |||||
Philosopher[] philosophers = new Philosopher[NUM_PHILOSOPHERS]; | |||||
Fork[] forks = new Fork[NUM_PHILOSOPHERS]; | |||||
// Initialisiere Gabeln | |||||
for (int i = 0; i < NUM_PHILOSOPHERS; i++) { | |||||
forks[i] = new Fork(i); | |||||
} | |||||
// Initialisiere Philosophen | |||||
for (int i = 0; i < NUM_PHILOSOPHERS; i++) { | |||||
Fork leftFork = forks[i]; | |||||
Fork rightFork = forks[(i + 1) % NUM_PHILOSOPHERS]; | |||||
// Um Deadlock zu vermeiden, greift der letzte Philosoph zuerst zur rechten Gabel. | |||||
if (i == NUM_PHILOSOPHERS - 1) { | |||||
philosophers[i] = new Philosopher(i, rightFork, leftFork); | |||||
} else { | |||||
philosophers[i] = new Philosopher(i, leftFork, rightFork); | |||||
} | |||||
new Thread(philosophers[i], "Philosopher-" + i).start(); | |||||
} | |||||
} | |||||
} | |||||
class Fork { | |||||
private final int id; | |||||
private boolean inUse; | |||||
// Konstruktor | |||||
public Fork(int id) { | |||||
this.id = id; | |||||
this.inUse = false; | |||||
} | |||||
// Liefert die ID der Gabel zurück | |||||
public int getId() { | |||||
return id; | |||||
} | |||||
// Gabel aufnehmen | |||||
public synchronized boolean take() { | |||||
if (!inUse) { | |||||
inUse = true; // Gabel ist jetzt in Benutzung | |||||
try { | |||||
Thread.sleep(1000); // 1 Sekunde warten, um die Gabel aufzunehmen | |||||
} catch (InterruptedException e) { | |||||
Thread.currentThread().interrupt(); | |||||
} | |||||
return true; | |||||
} | |||||
return false; // Gabel ist bereits in Benutzung | |||||
} | |||||
// Gabel freigeben | |||||
public synchronized void put() { | |||||
inUse = false; // Gabel freigeben | |||||
} | |||||
@Override | |||||
public String toString() { | |||||
return "Fork-" + id; | |||||
} | |||||
} | |||||
class Philosopher implements Runnable { | |||||
private final int id; | |||||
private final Fork leftFork; | |||||
private final Fork rightFork; | |||||
public Philosopher(int id, Fork leftFork, Fork rightFork) { | |||||
this.id = id; | |||||
this.leftFork = leftFork; | |||||
this.rightFork = rightFork; | |||||
} | |||||
@Override | |||||
public void run() { | |||||
try { | |||||
// Linke Gabel aufnehmen | |||||
System.out.println("Philosopher-" + id + " is trying to get " + leftFork); | |||||
while (!leftFork.take()) { | |||||
Thread.yield(); // Busy waiting entschärfen | |||||
} | |||||
System.out.println("Philosopher-" + id + " got " + leftFork); | |||||
// Rechte Gabel aufnehmen | |||||
System.out.println("Philosopher-" + id + " is trying to get " + rightFork); | |||||
while (!rightFork.take()) { | |||||
Thread.yield(); // Busy waiting entschärfen | |||||
} | |||||
System.out.println("Philosopher-" + id + " got " + rightFork); | |||||
// Philosoph isst | |||||
System.out.println("Philosopher-" + id + " is eating."); | |||||
Thread.sleep(2000); // 2 Sekunden essen | |||||
// Gabeln freigeben | |||||
leftFork.put(); | |||||
System.out.println("Philosopher-" + id + " put down " + leftFork); | |||||
rightFork.put(); | |||||
System.out.println("Philosopher-" + id + " put down " + rightFork); | |||||
// Philosoph beendet das Essen | |||||
System.out.println("Philosopher-" + id + " finished eating."); | |||||
} catch (InterruptedException e) { | |||||
Thread.currentThread().interrupt(); | |||||
System.out.println("Philosopher-" + id + " was interrupted."); | |||||
} | |||||
} | |||||
} | |||||
import java.io.BufferedReader; | |||||
import java.io.InputStreamReader; | |||||
import java.io.PrintWriter; | |||||
import java.net.Socket; | |||||
import java.util.Scanner; | |||||
public class DistributedSorterClient { | |||||
public static void main(String[] args) { | |||||
new DistributedSorterClient().run(); | |||||
} | |||||
private void run() { | |||||
Scanner scanner = new Scanner(System.in); | |||||
while (true) { | |||||
System.out.print("Enter a string to sort (or press Enter to quit): "); | |||||
String line = scanner.nextLine(); | |||||
if (line.isEmpty()) break; | |||||
// Senden und Empfangen der sortierten Zeile | |||||
String sortedLine = sort(line); | |||||
if (sortedLine != null) { | |||||
System.out.println("Sorted: " + sortedLine); | |||||
} else { | |||||
System.out.println("Error communicating with server."); | |||||
} | |||||
} | |||||
scanner.close(); | |||||
} | |||||
private String sort(String line) { | |||||
String response = ""; | |||||
try (Socket socket = new Socket("localhost", 12345); | |||||
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |||||
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { | |||||
// Sende die Eingabezeile an den Server | |||||
out.println(line); | |||||
// Warte auf die Antwort des Servers | |||||
response = in.readLine(); | |||||
} catch (Exception e) { | |||||
System.err.println("Error communicating with server: " + e.getMessage()); | |||||
return null; | |||||
} | |||||
return response; | |||||
} | |||||
} |
import java.io.BufferedReader; | |||||
import java.io.InputStreamReader; | |||||
import java.io.PrintWriter; | |||||
import java.net.ServerSocket; | |||||
import java.net.Socket; | |||||
import java.util.Arrays; | |||||
public class DistributedSorterServer implements Runnable { | |||||
private final Socket clientSocket; | |||||
// Konstruktor | |||||
public DistributedSorterServer(Socket clientSocket) { | |||||
this.clientSocket = clientSocket; | |||||
} | |||||
public static void main(String[] args) { | |||||
final int port = 12345; | |||||
try (ServerSocket serverSocket = new ServerSocket(port)) { | |||||
System.out.println("Distributed Sorter Server is running on port " + port); | |||||
while (true) { | |||||
// Warten auf neue Verbindung | |||||
Socket client = serverSocket.accept(); | |||||
System.out.println("New client connected: " + client.getInetAddress()); | |||||
// Neuen Thread starten, um die Client-Anfrage zu bearbeiten | |||||
Thread thread = new Thread(new DistributedSorterServer(client)); | |||||
thread.start(); | |||||
} | |||||
} catch (Exception e) { | |||||
System.err.println("Error: " + e.getMessage()); | |||||
} | |||||
} | |||||
@Override | |||||
public void run() { | |||||
try ( | |||||
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |||||
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true) | |||||
) { | |||||
String inputLine; | |||||
// Verarbeite alle Nachrichten vom Client | |||||
while ((inputLine = in.readLine()) != null) { | |||||
System.out.println("Received: " + inputLine); | |||||
// Sortiere die Buchstaben im String | |||||
String sortedLine = sortCharacters(inputLine); | |||||
System.out.println(inputLine + " -> " + sortedLine); | |||||
// Antwort zurück an den Client senden | |||||
out.println(sortedLine); | |||||
} | |||||
} catch (Exception e) { | |||||
System.err.println("Error handling client: " + e.getMessage()); | |||||
} finally { | |||||
try { | |||||
clientSocket.close(); | |||||
System.out.println("Client disconnected."); | |||||
} catch (Exception e) { | |||||
System.err.println("Error closing client socket: " + e.getMessage()); | |||||
} | |||||
} | |||||
} | |||||
// Sortiermethode für die Zeichen im String | |||||
private String sortCharacters(String input) { | |||||
char[] chars = input.toCharArray(); | |||||
Arrays.sort(chars); | |||||
return new String(chars); | |||||
} | |||||
} |