1 18 19 package com.martiansoftware.nailgun.examples; 20 21 import com.martiansoftware.nailgun.NGContext; 22 import com.martiansoftware.nailgun.NGServer; 23 24 43 public class Stack { 44 45 private static java.util.Stack sharedStack = new java.util.Stack (); 46 private static boolean done = false; 47 48 public static void nailShutdown(NGServer server) { 49 done = true; 50 synchronized(sharedStack) { 51 sharedStack.notifyAll(); 52 } 53 } 54 55 public static void nailMain(NGContext context) throws InterruptedException { 56 if (context.getCommand().equals("push")) { 57 synchronized(sharedStack) { 58 String [] args = context.getArgs(); 59 for (int i = 0; i < args.length; ++i) { 60 sharedStack.push(args[i]); 61 } 62 sharedStack.notifyAll(); 63 context.exit(0); 64 return; 65 } 66 } else if (context.getCommand().equals("pop")) { 67 int exitCode = 1; 68 synchronized(sharedStack) { 69 while (!done && (sharedStack.size() == 0)) { 70 sharedStack.wait(); 71 } 72 if (sharedStack.size() > 0) { 73 context.out.println(sharedStack.pop()); 74 exitCode = 0; 75 } 76 } 77 context.exit(exitCode); 78 return; 79 } 80 } 81 } 82 | Popular Tags |