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