summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/BroadcasterImpl.java
blob: 27b85398fde90aec8498f80791ae25632881245c (plain)
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
package com.orbekk.same;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class BroadcasterImpl implements Broadcaster {
    private Executor executor;

    /**
     * Get a BroadcastRunner for ClientService using a thread pool of size 20.
     */
    public static BroadcasterImpl getDefaultBroadcastRunner() {
        return new BroadcasterImpl(Executors.newFixedThreadPool(20));
    }

    public BroadcasterImpl(Executor executor) {
        this.executor = executor;
    }

    @Override
    public synchronized void broadcast(final List<String> targets,
            final ServiceOperation operation) {
        for (final String t : targets) {
            executor.execute(new Runnable() {
                @Override public void run() {
                    operation.run(t);
                }
            });
        }
    }
}