-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadUtils.scala
197 lines (158 loc) · 6.04 KB
/
ThreadUtils.scala
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package japgolly.webapputil.general
import com.typesafe.scalalogging.Logger
import java.time.Duration
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{Executors, LinkedBlockingQueue, ThreadFactory, ThreadPoolExecutor, TimeUnit}
import java.util.{Timer => JTimer, TimerTask}
import scala.concurrent.ExecutionContext
object ThreadUtils {
object ThreadGroups {
val scheduledTasks = new ThreadGroup("ScheduledTasks")
val shutdown = new ThreadGroup("Shutdown")
}
def runOnShutdown(name: String)(proc: => Unit): Unit = {
val t = new Thread(ThreadGroups.shutdown, () => proc, "shutdown-" + name)
java.lang.Runtime.getRuntime.addShutdownHook(t)
}
def runOnShutdown[F[_]](name: String, task: F[Unit])(implicit F: Effect.Sync[F]): Unit =
runOnShutdown(name)(F.runSync(task))
def newThreadFactory(groupName: String): ThreadFactory = {
val group = new ThreadGroup(groupName)
val count = new AtomicInteger(0)
val prefix = groupName + "-thread-"
new ThreadFactory {
override def newThread(r: Runnable) = {
val name = prefix + count.incrementAndGet()
val t = new Thread(group, r, name)
t.setDaemon(true)
t
}
}
}
def newThreadPoolExecutor(threads: Int, name: String): ThreadPoolExecutor =
newThreadPoolExecutor(threads, newThreadFactory(name))
def newThreadPoolExecutor(threads: Int, threadFactory: ThreadFactory): ThreadPoolExecutor = {
val e = new ThreadPoolExecutor(
threads,
threads,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue[Runnable],
threadFactory)
e.prestartAllCoreThreads()
e
}
implicit class ExecutionContextExt(private val ec: ExecutionContext) extends AnyVal {
def onUncaughtError(f: Throwable => Unit): ExecutionContext =
new ExecutionContext {
def execute(r: Runnable): Unit =
ec.execute { () =>
try
r.run()
catch {
case e: Throwable => f(e)
}
}
def reportFailure(cause: Throwable): Unit =
ec.reportFailure(cause)
}
def logUncaughtErrors(l: Logger): ExecutionContext =
onUncaughtError(e => l.error("Uncaught error in ExecutionContext: {}", e.toString, e))
}
// ===================================================================================================================
def newThreadPool(threadGroupName: String, logger: Logger): ThreadPool =
new ThreadPool(threadGroupName, newThreadFactory(threadGroupName), logger)
final class ThreadPool(threadGroupName: String, threadFactory: ThreadFactory, logger: Logger) {
def withThreads(n: Int) = ThreadPool2(threadGroupName, newThreadPoolExecutor(n, threadFactory), logger)
def withMaxThreads() = withThreads(java.lang.Runtime.getRuntime.availableProcessors())
}
final case class ThreadPool2(threadGroupName: String, threadPoolExecutor: ThreadPoolExecutor, logger: Logger) {
def executionContext: ExecutionContext =
ExecutionContext.fromExecutor(threadPoolExecutor).logUncaughtErrors(logger)
def autoShutdown(): ThreadPool2 = {
runOnShutdown(threadGroupName)(threadPoolExecutor.shutdown())
this
}
def shutdown[F[_]](implicit F: Effect.Sync[F]): F[Unit] =
F.delay(threadPoolExecutor.shutdown())
}
// ===================================================================================================================
def newScheduler(threadName: String, threadGroup: ThreadGroup): Scheduler =
new Scheduler(threadName, threadGroup)
final class Scheduler(threadName: String, threadGroup: ThreadGroup) {
val executorService =
Executors.newSingleThreadScheduledExecutor(new Thread(threadGroup, _, threadName))
def scheduleAtFixedRate[F[_]: Effect.Sync, A](f: F[A], period: Duration): Scheduler =
scheduleAtFixedRate(f, period, period)
def scheduleAtFixedRate[F[_], A](f: F[A], initialDelay: Duration, period: Duration)(implicit F: Effect.Sync[F]): Scheduler = {
executorService.scheduleAtFixedRate(
() => F.runSync(f),
initialDelay.toMillis,
period.toMillis,
TimeUnit.MILLISECONDS)
this
}
def addShutdownHook(proc: => Unit): Scheduler = {
runOnShutdown(threadName)(proc)
this
}
def addShutdownHook[F[_]: Effect.Sync](f: F[Unit]): Scheduler = {
runOnShutdown(threadName, f)
this
}
}
// ===================================================================================================================
def unsafeRunWithTimeLimit[A](maxDur: Duration)(task: => A): Option[A] = {
val lock = new AnyRef
val sync = new AnyRef
var result = Option.empty[A]
var done = false
val timer = new JTimer("unsafeRunWithTimeLimit", true)
var thread = null: Thread
def complete(r: Option[A]) = {
val notify =
lock.synchronized {
if (done)
false
else {
done = true
result = r
if (r.isDefined)
timer.cancel()
else if (thread ne null)
thread.interrupt()
true
}
}
if (notify)
sync.synchronized {
sync.notify()
}
}
try {
val taskRunnable: Runnable = () =>
try {
val a = task
complete(Some(a))
} catch {
case _: InterruptedException =>
} finally
complete(None)
val timeout = new TimerTask {
override def run(): Unit =
complete(None)
}
timer.schedule(timeout, maxDur.toMillis)
// Note: it's important that this task start *after* the call to timer.schedule above.
// Otherwise this thread can complete and cancel the timer before the .schedule call, which results in .schedule
// throwing a runtime exception because the timer has already been cancelled.
thread = new Thread(taskRunnable)
thread.start()
sync.synchronized {
sync.wait(maxDur.toMillis)
}
result
} finally
timer.cancel()
}
}