Servus Community,
Ich hab hier ein kleines Problem und komm nicht mehr weiter. Was sagt ihr zu meinem Code bis jetzt. Folgende Angabe:
Hier mein Code bis jetzt:
Ich würde mich über eine Antwort freuen
mfg El Hadji
Ich hab hier ein kleines Problem und komm nicht mehr weiter. Was sagt ihr zu meinem Code bis jetzt. Folgende Angabe:
Hier mein Code bis jetzt:
Code:
package threads;
import threads.geg.Receiver;
import java.util.*;
public class Communicator implements Receiver
{
private HashMap <String, Receiver> liste;
private static Object lock = new Object();
private static Object lock2 = new Object();
private int zaehler;
private String name;
private int send;
private int sendAll;
public Communicator()
{
liste = new HashMap<String,Receiver>();
zaehler = 0;
name = "Receiver";
send = 0;
sendAll =0;
}
public void receive(String message)
{
System.out.println(message);
}
public synchronized String signUp(Receiver aReceiver)
{
if(liste.containsValue(aReceiver))
{
return name;
}
zaehler++;
name = "Receiver" + zaehler;
liste.put(name, aReceiver);
return name;
}
public void send(String identifier, String message)
{
synchronized (lock)
{
while(sendAll > 0)
{
try
{
wait();
send--;
}
catch(InterruptedException e)
{
throw new RuntimeException(e);
}
}
if(!liste.containsKey(identifier))
{
throw new RuntimeException("Invalid identifier");
}
liste.get(identifier).receive(message);
send++;
this.notifyAll();
}
}
public synchronized void sendAll(String message)
{
while(send > 0)
{
try
{
wait();
sendAll--;
}
catch(InterruptedException e)
{
throw new RuntimeException(e);
}
}
Set<String> neu = liste.keySet();
for(String f : neu)
{
liste.get(f).receive(message);
}
sendAll++;
this.notifyAll();
}
}
Ich würde mich über eine Antwort freuen
mfg El Hadji