玩具语言java

时间:2015-12-28 19:13:04

标签: java multithreading

我必须使用以下规则在我的玩具语言中实现fork语句:

ExeStack1={fork(Stmt1) | Stmt2|Stmt3|....}
SymTable1,
Heap1,
Out1,
id1
==>
ExeStack2={Stmt2 | Stmt3|.....}
SymTable2=SymTable1
Heap2 = Heap1
Out2 = Out1
id2=id1
and a new PrgSate is created with the following data structures:
ExeStack3=Stmt1
SymTable3=clone(SymTable1)
Heap3=Heap1,
Out3=Out1
id3= id1*10 in order to be unique

有这样的说明:执行方法返回新的PrgState。如上所示,当fork语句位于ExeStack之上时,会生成一个新的PrgState(线程),其中包含fork的参数作为ExeStack,因为SymTable是父PrgState(父线程)SymTable的克隆,如Heap a引用父PrgState(父线程)Heap和Out引用父PrgState(父线程)Out。请注意,Heap和Out由所有PrgStates共享。新线程的SymTable是克隆(或新副本)而且是 不与父线程共享。 我实现了这样的事情:

  public class ForkStmt implements IStmt,Serializable{
  IStmt stmt;
  public ForkStmt(IStmt st){
      stmt= st;
  }
  public IStmt getStmt(){
      return stmt;
  }
  public String toString(){
      return "fork(" + stmt + ")";
  }
  public PrgState execute(PrgState prg){

      IStack<IStmt> a =prg.getStk();
      a = (IStack<IStmt>)stmt;
      Id<Object,Integer> dictRef = prg.getDict();

      IHeap<Integer,Integer> h = prg.getHeap();
      IList<Object> l = prg.getList();
      PrgState forkPrg = new PrgState(a,dictRef,l,h);
    return forkPrg;

  }

}

好实施?谢谢。

0 个答案:

没有答案