diningphilosopherscollege.java

来自「升级版本」· Java 代码 · 共 56 行

JAVA
56
字号
import org.jcsp.lang.Any2OneChannel;import org.jcsp.lang.CSProcess;import org.jcsp.lang.ChannelOutput;import org.jcsp.lang.One2OneChannel;import org.jcsp.lang.Parallel;import org.jcsp.lang.Channel;class DiningPhilosophersCollege implements CSProcess {  private final int nPhilosophers;  private final ChannelOutput report;  public DiningPhilosophersCollege (int nPhilosophers, ChannelOutput report) {    this.nPhilosophers = nPhilosophers;    this.report = report;  }  public void run () {    final One2OneChannel[] left = Channel.one2oneArray(nPhilosophers);    final One2OneChannel[] right = Channel.one2oneArray(nPhilosophers);    final Any2OneChannel down = Channel.any2one();        final Any2OneChannel up = Channel.any2one();    final Fork[] fork = new Fork[nPhilosophers];    for (int i = 0; i < nPhilosophers; i++) {      fork[i] = new Fork (nPhilosophers, i,                          left[i].in(), right[(i + 1)%nPhilosophers].in(), report);    }    final Philosopher[] phil = new Philosopher[nPhilosophers];    for (int i = 0; i < nPhilosophers; i++) {      phil[i] = new Philosopher (i, left[i].out(), right[i].out(), down.out(), up.out(), report);    }    new Parallel (      new CSProcess[] {        new Parallel (phil),        new Parallel (fork),        new Security (down.in(), up.in(), report),        new Clock (report)      }    ).run ();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?