34 lines
726 B
Kotlin
34 lines
726 B
Kotlin
|
package drills.drill06.exercise4.actuator
|
||
|
|
||
|
import java.util.concurrent.CountDownLatch
|
||
|
import java.util.concurrent.TimeUnit
|
||
|
|
||
|
class Actuator(private val action: Action) : Thread() {
|
||
|
var latch = CountDownLatch(1)
|
||
|
|
||
|
override fun run() {
|
||
|
while (true) {
|
||
|
latch.await()
|
||
|
|
||
|
synchronized(this) {
|
||
|
latch = CountDownLatch(1)
|
||
|
}
|
||
|
|
||
|
if (action.getState() == Action.State.OFF)
|
||
|
action.on()
|
||
|
|
||
|
latch.await(2050, TimeUnit.MILLISECONDS)
|
||
|
|
||
|
synchronized(this) {
|
||
|
if (latch.count == 1L)
|
||
|
action.off()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Synchronized
|
||
|
fun actuate() {
|
||
|
latch.countDown()
|
||
|
}
|
||
|
}
|