📄 stagedemo.java
字号:
/* Copyright (c) 2003, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.*/package gov.lbl.dsd.sea.demo;import gov.lbl.dsd.sea.EventHandler;import gov.lbl.dsd.sea.Stage;import gov.lbl.dsd.sea.StageManager;import gov.lbl.dsd.sea.event.IllegalEventException;import java.util.Date;import EDU.oswego.cs.dl.util.concurrent.CountDown;/** * Demonstrates how to use the framework with a very basic example. In this * example there are two stages. The first stage receives number events that are * incrementing over time. It prints the number, adds 1000 to it, and hands the * new number to the second stage, which also prints it. * * @author whoschek@lbl.gov * @author $Author: gegles $ * @version $Revision: 1.4 $, $Date: 2004/09/16 16:57:15 $ */public class StageDemo { public static void main(String[] args) { int runs = 3; if (args.length > 0) runs = Integer.parseInt(args[0]); final CountDown barrier = new CountDown(1); // a barrier to later wait until all responses have arrived // the event handler for the second stage EventHandler h2 = new EventHandler() { public void handle(Object event) { System.out.println("handler2: " + event); int val = ((Integer) event).intValue(); if (val == -1+1000) { barrier.release(); } } }; StageManager manager = new StageManager(); final Stage s2 = manager.createStage(h2).start(); // the event handler for the first stage EventHandler h1 = new EventHandler() { public void handle(Object event) { System.out.println("handler1: " + event); if (! (event instanceof Integer)) throw new IllegalEventException(event, this.getStage()); int val = ((Integer) event).intValue(); s2.enqueue(new Integer(val + 1000)); } }; Stage s1 = manager.createStage(h1).start(); // enqueue some events to be handled immediately, and also later in three secs s1.enqueue(new Integer(100), new Date(System.currentTimeMillis() + 1)); s1.enqueue(new Integer(200), new Date(System.currentTimeMillis() + 3000)); //s1.enqueue("illegal dummy"); // uncomment this to experiment with getting IllegalEventExceptions // enqueue some more events to be handled ASAP for (int i=0; i < runs; i++) { s1.enqueue(new Integer(i)); } /* // uncomment to see the "three second" event being handled before shutting down try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } */ s1.enqueue(new Integer(-1)); // enqueue crude "termination signal" // cleanly shut down all stages and threads System.out.println("waiting for all events to run through pipeline..."); try { barrier.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("now shutting down..."); manager.stopAll(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -