Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Dining Philosophers" using Fututre in Java
    primarykey
    data
    text
    <p>I am trying to solve Philosopher Problem using Future in Java.</p> <pre><code> public class Philo implements Callable&lt;PhiloStatus&gt; { private PhiloStatus status; private Philo leftPhilo; private Philo rightPhilo; private String name; public Philo(String name) { status = PhiloStatus.THINKING; this.name =name; } public void setLeftPhilo(Philo leftPhilo) { this.leftPhilo = leftPhilo; } enter code here public void setRightPhilo(Philo rightPhilo) { this.rightPhilo = rightPhilo; } @Override public PhiloStatus call() throws Exception { if (leftPhilo.getStatus() == PhiloStatus.THINKING &amp;&amp; rightPhilo.getStatus() == PhiloStatus.THINKING) { this.status =PhiloStatus.DINING; System.out.println("Dininig "+this); return PhiloStatus.DINING; } this.status =PhiloStatus.THINKING; System.out.println("Thinking "+this); return PhiloStatus.THINKING; } public PhiloStatus getStatus() { return status; } @Override public String toString() { // TODO Auto-generated method stub return name; } } </code></pre> <p>Starting point of the program</p> <pre><code>public class Start { /** * @param args */ public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(5); Philo[] philosophers = new Philo[5]; for (int i = 0; i &lt; 5; ++i) philosophers[i] = new Philo(""+i); for (int i = 0; i &lt; 5; ++i) { philosophers[i].setLeftPhilo(philosophers[(i + 4) % 5]); philosophers[i].setRightPhilo(philosophers[(i + 1) % 5]); executorService.submit( philosophers[i]); } } } </code></pre> <p>But seems like once Callable is finished executing , it returns the same result.</p> <p>I doubt now that it might not be possible to solve this problem using Future? </p> <p>Could anyone please shed a light on this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload