您的位置:程序门 -> java -> j2se / 基础类



内部类编译时问题?????


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


内部类编译时问题?????
发表于: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,
发表于:2007-01-30 15:39:481楼 得分:0
该回复于2007-12-28 11:30:37被管理员或版主删除
发表于:2007-01-30 21:32:052楼 得分:0
第一个问题我也不清楚
第二个问题:
string[]   args实际上就是你运行程序时输入的参数
发表于:2007-01-30 23:59:263楼 得分:0
2.java   simplethreads   3
3.interrupt()使程序跳出for循环进入catch块
发表于:2007-01-31 08:14:444楼 得分:0
我编译出来只有两个。


快速检索

最新资讯
热门点击