view src/alice/test/dpp/codesegment/ThinkCodeSegment.java @ 12:bfaaebb50a2e

modified Fork.java
author one
date Sun, 25 Nov 2012 18:49:29 +0900
parents 2794091a7058
children 888388e47fe5
line wrap: on
line source

package alice.test.dpp.codesegment;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;
import alice.test.dpp.model.Fork;
import alice.test.dpp.model.Philosophy;

public class ThinkCodeSegment extends CodeSegment {

	Philosophy phil;
	Receiver arg1 = ids.create(CommandType.TAKE);
	Receiver arg2 = ids.create(CommandType.TAKE);

	public ThinkCodeSegment(Philosophy p) {
		phil = p;
		arg1.setKey(phil.getLeftFork());
		arg2.setKey(phil.getRightFork());			
	}

	@Override
	public void run() {
		System.out.println(phil.getName() + " thinking");
		Fork leftFork = arg1.asClass(Fork.class);
		Fork rightFork = arg2.asClass(Fork.class);
		double rand = Math.random();
		if (rand < 0.5) {
			try {
				Thread.sleep(10);
				new ThinkCodeSegment(phil);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {
			new TakeForkCodeSegment(phil);
		}

		ods.update("local", leftFork.getForkName(), leftFork);
		ods.update("local", rightFork.getForkName(), rightFork);
	}
	
}