Java Semaphore - Print even and odd numbers using Threads
In previous chapter we saw what are semaphores and how to use them. In this chapter we will be implementing a program to print even and odd numbers using semaphore. We will be creating a semaphore with single permit. This semaphore will be shared by 2 threads. One thread will print even numbers and the other will print odd numbers.
Semaphore - Table Of Contents
Understanding Java Semaphore Java Semaphore - Print even and odd numbers using Threads Java Semaphore - Print numbers sequence using three Threads
-
          Step 1                           Step2
    -
          Step 3                           Step4
      
package com.javastructures; import java.util.concurrent.Semaphore; public class SemaphoreExample { public static void main(String[] args) { //We dont want the even number printed first so the initial permit for this semaphore is 0. Semaphore evenSemaphore = new Semaphore(0); //We want to print the odd number first so the initial permit for this semaphore is 1. Semaphore oddSemaphore = new Semaphore(1); PrintOddNumber printOddNumber = new PrintOddNumber(evenSemaphore, oddSemaphore); PrintEvenNumber printEvenNumber = new PrintEvenNumber(evenSemaphore, oddSemaphore); new Thread(printOddNumber, "Print Odd").start(); new Thread(printEvenNumber, "Print Odd").start(); } } class PrintEvenNumber implements Runnable { Semaphore sempahoreEven; Semaphore sempahoreOdd; public PrintEvenNumber(Semaphore sempahoreEven, Semaphore sempahoreOdd) { super(); this.sempahoreEven = sempahoreEven; this.sempahoreOdd = sempahoreOdd; } @Override public void run() { for (int i = 2;; i = i + 2) { try { //This will decrement the permit used by the even semaphore to 0. sempahoreEven.acquire(); System.out.println(i); //This will increment the permit used by odd semaphore by 1. sempahoreOdd.release(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class PrintOddNumber implements Runnable { Semaphore sempahoreEven; Semaphore sempahoreOdd; public PrintOddNumber(Semaphore sempahoreEven, Semaphore sempahoreOdd) { this.sempahoreEven = sempahoreEven; this.sempahoreOdd = sempahoreOdd; } @Override public void run() { for (int i = 1;; i = i + 2) { try { //This will decrement the permit used by the odd semaphore to 0. sempahoreOdd.acquire(); Thread.sleep(1000); System.out.println(i); //This will increment the permit used by even semaphore by 1. sempahoreEven.release(); } catch (InterruptedException e) { e.printStackTrace(); } } } }Run the program as a Java Application -