| 发表于:2007-01-30 15:28:12 楼主 |
public class simplethreads { //display a message, preceded by the name of the current thread static void threadmessage(string message) { string threadname = thread.currentthread().getname(); system.out.format( "%s: %s%n ", threadname, message); } private static class messageloop implements runnable { public void run() { string importantinfo[] = { "mares eat oats ", "does eat oats ", "little lambs eat ivy ", "a kid will eat ivy too " }; try { for (int i = 0; i < importantinfo.length; i++) { //pause for 4 seconds thread.sleep(4000); //print a message threadmessage(importantinfo[i]); } } catch (interruptedexception e) { threadmessage( "i wasn 't done! "); } } } public static void main(string args[]) throws interruptedexception { //delay, in milliseconds before we interrupt messageloop //thread (default one hour). long patience = 1000 * 60 * 60; //if command line argument present, gives patience in seconds. if (args.length > 0) { try { patience = long.parselong(args[0]) * 1000; } catch (numberformatexception e) { system.err.println( "argument must be an integer. "); system.exit(1); } } threadmessage( "starting messageloop thread "); long starttime = system.currenttimemillis(); thread t = new thread(new messageloop()); t.start(); threadmessage( "waiting for messageloop thread to finish "); //loop until messageloop thread exits while (t.isalive()) { threadmessage( "still waiting... "); //wait maximum of 1 second for messageloop thread to //finish. t.join(1000); if (((system.currenttimemillis() - starttime) > patience) && t.isalive()) { threadmessage( "tired of waiting! "); t.interrupt(); //shouldn 't be long now -- wait indefinitely t.join(); } } threadmessage( "finally! "); } } 1,这就一个文件可编译后怎么出来三个class呀??? 而它只有一个内部类。 2,此段程序中如何在运行时通过string [] args来输入信息??? 3,此段程序中怎么来体现中方法interrupt的调用??? 4, |
|
|
|
|