lm-sistemi-software-distrib.../src/main/kotlin/drills/drill06/exercise4/actuator/Actuator.kt

34 lines
726 B
Kotlin
Raw Normal View History

2020-04-21 14:33:19 +00:00
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()
}
}