1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import java.util.*;
import java.util.concurrent.*;
import java.lang.*;
import java.io.*;
// J'ai mocké ta classe Directory
class Directory {
private static Map<String, String> map = Collections.synchronizedMap(new HashMap<>());
public void put(String key, String value){
map.put(key, value);
}
public Map<String, String> getMap(){
return map;
}
}
class DemoClients {
public final static int SIZE = 3; // Nombre de client
public static void main (String[] args) throws java.lang.Exception {
Directory d = new Directory(); // <=> Naming.lookup("rmi://localhost/directory");
// L'action que doit effectuer chacun de tes clients est représenté par un callable
Callable<Object>[] callables = new Callable[SIZE];
for(int i = 0; i < SIZE; i++){
final int i2 = i;
callables[i] = () -> {
d.put("name"+i2, "value"+i2);
return null;
};
}
// Exécution des actions de tes clients via un pool de thread (cf: non-séquentiel)
ExecutorService es = Executors.newFixedThreadPool(SIZE);
es.invokeAll(Arrays.asList(callables));
es.shutdown();
// Affichage
for (Map.Entry<String, String> e : d.getMap().entrySet()) {
System.out.println("Key : " + e.getKey() + " Value : " + e.getValue());
}
}
} |