Jump to content

Metered concurrency: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 928:
}
}</lang>
 
=={{header|Kotlin}}==
<lang scala>// version 1.1.51
 
import java.util.concurrent.Semaphore
import kotlin.concurrent.thread
 
fun main(args: Array<String>) {
val numPermits = 4
val numThreads = 9
val semaphore = Semaphore(numPermits)
for (i in 1..numThreads) {
thread {
val name = "Unit #$i"
semaphore.acquire()
println("$name has acquired the semaphore")
Thread.sleep(2000)
semaphore.release()
println("$name has released the semaphore")
}
}
}</lang>
 
Sample output:
<pre>
Unit #1 has acquired the semaphore
Unit #2 has acquired the semaphore
Unit #3 has acquired the semaphore
Unit #4 has acquired the semaphore
Unit #1 has released the semaphore
Unit #5 has acquired the semaphore
Unit #2 has released the semaphore
Unit #6 has acquired the semaphore
Unit #4 has released the semaphore
Unit #8 has acquired the semaphore
Unit #3 has released the semaphore
Unit #7 has acquired the semaphore
Unit #5 has released the semaphore
Unit #6 has released the semaphore
Unit #9 has acquired the semaphore
Unit #8 has released the semaphore
Unit #7 has released the semaphore
Unit #9 has released the semaphore
</pre>
 
=={{header|Logtalk}}==
9,492

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.