changeset 77:481f322b3206 working

add test concurrent execution of code segment
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Wed, 29 Feb 2012 22:45:00 +0900
parents 4a2ecd0a5e8f
children 6d0086a503d3
files src/alice/test/concurrent/AliceConcurrentTest.java src/alice/test/concurrent/AliceFinish.java src/alice/test/concurrent/SleepCodeSegment.java src/alice/test/concurrent/StartConcurrent.java
diffstat 4 files changed, 106 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/concurrent/AliceConcurrentTest.java	Wed Feb 29 22:45:00 2012 +0900
@@ -0,0 +1,18 @@
+package alice.test.concurrent;
+
+public class AliceConcurrentTest {
+
+	public static void main(String args[]) {
+		
+		int count = 1;
+		
+		for (int i = 0; i< args.length; i++) {
+			if ("-c".equals(args[i])) {
+				count = Integer.parseInt(args[++i]);
+			}
+		}
+		
+		new StartConcurrent(count).execute();
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/concurrent/AliceFinish.java	Wed Feb 29 22:45:00 2012 +0900
@@ -0,0 +1,25 @@
+package alice.test.concurrent;
+
+import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+public class AliceFinish extends CodeSegment {
+
+	private int num;
+	
+	public Receiver ds1 = ids.create(CommandType.TAKE);
+	
+	public AliceFinish(int threadNum) {
+		this.num = threadNum; 
+	}
+
+	@Override
+	public void run() {
+		if (--num == 0) System.exit(0);
+		
+		AliceFinish cs1 = new AliceFinish(num);
+		cs1.ds1.setKey("finish");
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/concurrent/SleepCodeSegment.java	Wed Feb 29 22:45:00 2012 +0900
@@ -0,0 +1,31 @@
+package alice.test.concurrent;
+
+import org.msgpack.type.ValueFactory;
+
+import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+public class SleepCodeSegment extends CodeSegment {
+
+	public Receiver ds1 = ids.create(CommandType.TAKE);
+	
+	private int num;
+	
+	public SleepCodeSegment(int i) {
+		this.num = i;
+	}
+
+	@Override
+	public void run() {
+		try {
+			Thread.sleep(1000);
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+		System.out.println("[CodeSegment-" + num + "] Hello, World!");
+		
+		ods.put("local", "finish", ValueFactory.createNilValue());
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/concurrent/StartConcurrent.java	Wed Feb 29 22:45:00 2012 +0900
@@ -0,0 +1,32 @@
+package alice.test.concurrent;
+
+import org.msgpack.type.ValueFactory;
+
+import alice.codesegment.CodeSegment;
+
+public class StartConcurrent extends CodeSegment {
+
+	private int count;
+	
+	public StartConcurrent(int count) {
+		this.count = count;
+	}
+
+	@Override
+	public void run() {
+		int threadNum = Runtime.getRuntime().availableProcessors() * count;
+		
+		AliceFinish cs1 = new AliceFinish(threadNum);
+		cs1.ds1.setKey("finish");
+		
+		for (int i = 0; i < threadNum; i++) {
+			SleepCodeSegment cs2 = new SleepCodeSegment(i);
+			cs2.ds1.setKey("wait");
+		}
+		
+		for (int i = 0; i < threadNum; i++) {
+			ods.put("local", "wait", ValueFactory.createNilValue());
+		}
+	}
+	
+}