Home java Incorrect semapore behavior in the simplest task

Incorrect semapore behavior in the simplest task

Author

Date

Category

Just understand with the concepts of Mutex and Semaphore, therefore I decided to solve the simplest problem about 5 philosophers.


About the task itself: there are 5 people (philosophers) and a table that enlists 2 people. It is necessary to feed all people and at the same time not to create a conflict situation when 2 people will try to sit at the table.


The idea of ​​the solution seems very simple: Create semiphore with 2 permit , and in run each stream deals with 1 of these Permit . After the delay occurs (the philosopher eats), the place is released – 1 permit is released.

full solution code, as it is relatively small:

import java.util.concurrent.semaphore;
Public Class MainsemapHore {
  Private Static Class Philosopher EXTENDS Thread {
    Private Semaphore Semaphore;
    Static int table_capacity = 0;
    Philosopher (String Name, Semaphore Semaphore) {
      This.setname (Name);
      this.semaphore = semapore;
    }
    @Override
    Public void Run () {
      super.run ();
      Try {
        System.out.Printf ("% s came to eat. \ N", getname ());
        semapore.acquire (1);
        Table_capacity ++;
        System.Out.printf ("% s sat at the table. At the table now:% D philosophers. \ N", getName (), Table_Capacity);
        Try {
          SLEEP (10);
        } Catch (InterruptedException E) {
          E.PrintStackTrace ();
        }
        Table_capacity--;
        System.out.Printf ("% s was rooted and got out of the table. At the table now:% D philosophers. \ N", getName (),
            Table_capacity);
        semaphore.release (1);
      } Catch (InterruptedException E) {
        E.PrintStackTrace ();
      }
      Finally {
        semaphore.release ();
      }
    }
  }
  Public Static Void Main (String [] Args) {
    Semaphore Sem = New Semaphore (2, True);
    Philosopher ph1 = new philosopher ("Aristotle", SEM);
    Philosopher ph2 = new philosopher ("Plato", SEM);
    Philosopher PH3 = New Philosopher (Socrates, SEM);
    Philosopher PH4 = New Philosopher ("Diogen", SEM);
    Philosopher PH5 = NEW PHILOSOPHER ("PROTAGOR", SEM);
    ph1.start ();
    ph2.start ();
    ph3.start ();
    pp4.start ();
    ph5.start ();
  }
}

Alas, in practice, the idea is not perfect. To be more accurate, it does not work completely. In the screenshot, it can be seen below that 3 philosophers are sitting at some point at some point:

Even if you reduce the number of places (permit ) to one, there will be a moment when the table will be 2 (and then 3) of the philosopher!

I absolutely do not understand what may be the problem.


Answer 1, Authority 100%

First Table_capacity Must be Atomicinteger , because there are two streams can be at the same time, and secondly you have two semaphore.release (1); one in try , other in finally and it turns out that one past philosophers fully frees semaphore

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions