Bonjour,
Je suis sur un petit problème de performances : je voudrais supprimer mon synchronized du code suivant en utilisant l'API java.util.concurrent pour gagner en performances. Avez-vous des idées de ce qui pourrait m'aider ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class MyService implements Runnable { private AtomicBoolean running = new AtomicBoolean(false); private final List<String> strings = new ArrayList<String>(); public void addString(String s) { strings.add(s); } public void removeString(String s) { strings.remove(s); } public void run () { if (running.compareAndSet(false, true)) { while (isRunning()) { List<String> copy; synchronized (strings) { copy = new ArrayList<String>(strings); } for (String s: copy) { // Traitement long et coûteux, d'où le synchronized précédent } } } } public boolean isRunning() { return running.get(); } public void stop() { running.compareAndSet(true, false); } }







Répondre avec citation
Partager