-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiThreading.java
49 lines (39 loc) · 1.69 KB
/
MultiThreading.java
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
46
47
48
49
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class MultiThreading {
public static final ExecutorService POOL = Executors.newFixedThreadPool(100, new ThreadFactory() {
final AtomicInteger counter = new AtomicInteger(0);
public Thread newThread(Runnable r) {
return new Thread(r, String.format("Thread %s", this.counter.incrementAndGet()));
}
});
public static final ScheduledExecutorService RUNNABLE_POOL = Executors.newScheduledThreadPool(10, new ThreadFactory() {
private final AtomicInteger counter = new AtomicInteger(0);
public Thread newThread(Runnable r) {
return new Thread(r, "Thread " + this.counter.incrementAndGet());
}
});
public static ScheduledFuture<?> schedule(Runnable r, long initialDelay, long delay, TimeUnit unit) {
return RUNNABLE_POOL.scheduleAtFixedRate(r, initialDelay, delay, unit);
}
public static ScheduledFuture<?> schedule(Runnable r, long delay, TimeUnit unit) {
return RUNNABLE_POOL.schedule(r, delay, unit);
}
public static void runAsync(Runnable runnable) {
POOL.execute(runnable);
}
public static int getTotal() {
ThreadPoolExecutor tpe = (ThreadPoolExecutor)POOL;
return tpe.getActiveCount();
}
public static void stopTask(){
POOL.shutdown();
RUNNABLE_POOL.shutdown();
}
}