Semaphores in Kotlin

Semaphore is a set of permits. Assume it as a box that has the tokens(permits) for attending(performing) a task. Threads have access to Semaphore but not those permits. Depending on the number of permits, and when a thread arrives at the semaphore, a permit is given. If a Thread arrives when there are no permits available, it has to wait until a permit is assigned to it. Only then a task can be performed.

So basically, A Semaphore is used in Multi-Threading or a Multi-Threaded application. There can be multiple processes running in an application. The use of Semaphore is to restrict or control access to any common resource by multiple threads(processes). Semaphore by definition is used for signaling from one task to another. They allow flexible source management.

Vizualization of Multi-Threaded process using a Semaphore

  1. Threads arriving at Semaphore for the resources.
    Vizualization of Multi-Threaded process
  2. Semaphore assigning Permits to the Threads and halts the rest of the Threads if falls short of Permits. The process of assigning Permit to a Thread is called Acquiring.
    Vizualization of Multi-Threaded process
  3. Previously running Threads release the Permits when the task is completed. These Permits are now assigned to the halted Threads. This process where the Threads submit back the Permit is called Releasing.
    Vizualization of Multi-Threaded process

The program below is in Kotlin, an example where a Semaphore is used for running some activity in a Multi-Threaded process:

  • The Semaphore is assigned a Permit value of 2 which means at a time only 2 Threads can run.
  • The Threads declared in the below code have a Sleep Activity of 10 seconds (10000 Millisecs) which means a Permit is acquired by any of the Threads for 10 seconds.
  • A print function is declared to print “.” to represent that Thread Activity is finished and the Permit is released.
  • Since there is no priority assigned to any of the Threads, a NORM_PRIORITY(Default Priority = 5) to all the Threads and Preference would be given to the Thread that arrives first.
filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.concurrent.Semaphore
  
val s = Semaphore(2)
  
val t1 = Thread(Runnable 
{
    s.acquireUninterruptibly()
    print("t1")
    Thread.sleep(10000)
    print(".")
    s.release()
})
  
val t2 = Thread(Runnable 
{
    s.acquireUninterruptibly()
    print("t2")
    Thread.sleep(10000)
    print(".")
    s.release()
})
  
val t3 = Thread(Runnable 
{
    s.acquireUninterruptibly()
    print("t3")
    Thread.sleep(10000)
    print(".")
    s.release()
})
  
val t4 = Thread(Runnable 
{
    s.acquireUninterruptibly()
    print("t4")
    Thread.sleep(10000)
    print(".")
    s.release()
})
  
  
t1.start()
t2.start()
t3.start()
t4.start()

chevron_right


Output:

t1t2..t3t4..

Here 2 dots i.e. in the output “..” represents 10 seconds wait(Thread.sleep) between permit acquire and permit release.

Note: Setting Priority to the Threads is possible. In such a case, the Threads are in Priority order and likewise, Permits are assigned to them. Multiple Semaphores can be declared depending upon the number of resources required by the process.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.