当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 上传图片和rm用fileinputstream好,还是filereader好写?? ......
 ·大家帮我出个主意    »显示摘要«
    摘要: 我做了一个显示动画的小程序。 动画保存为什么格式的文件好呢? ?gif ?avi 如何比较简单的实现? ......


关于一个非常简单线程问题

class   MyThread   extends   Thread  
  {  
          public   static   void   main(String   []   args)  
        {  
     MyThread   t=new   MyThread();  
                t.start();  
     System.out.println("one.");  
                t.start();  
              System.out.println("two.");  
        }  
      public   void   run()  
      {  
              System.out.println("thread");  
      }  
  }  
   
  为什么我执行的结果是:  
  D:\my>java   MyThread  
  one.  
  thread  
  two.  
   
  而不是:  
  thread  
  one  
  thread  
  two  
   
  而此题的答案是:  
  compilation   succeed   but   an   exception   is   thrown   at   line   7   when   run???  
  请帮我详细分析一下执行过程  
  谢谢了  
   
 

NO.1   作者: zosatapo

下面是SUN提供的说明,可以说是使用手册吧。  
   
  这个是规定的,没有理由  
   
        /**  
            *   Causes   this   thread   to   begin   execution;   the   Java   Virtual   Machine    
            *   calls   the   <code>run</code>   method   of   this   thread.    
            *   <p>  
            *   The   result   is   that   two   threads   are   running   concurrently:   the    
            *   current   thread   (which   returns   from   the   call   to   the    
            *   <code>start</code>   method)   and   the   other   thread   (which   executes   its    
            *   <code>run</code>   method).    
            *  
            *   @exception     IllegalThreadStateException     if   the   thread   was   already  
            *                               started.  
            *   @see                 java.lang.Thread#run()  
            *   @see                 java.lang.Thread#stop()  
            */  
 

NO.2   作者: muymuy

 
  首先Thread只能start()一次,  
  在你的程序中start()了两此,第一次是有效的,第二次就不会起任何作用。  
   
  线程start后,程序就出现了分支,这个分支就是你的线程,这样程序里就有两个线程,一个是主线程,一个是start后启动的线程,这两个线程并行运行,谁都可能先抢占到cpu,先运行,从你的运行结果来看,显然是主线程先运行,运行到一半的时候,子线程才开始运行,所以就会出现:  
  one.  
  thread  
  two.  
   
  改成下面的程序后,就会出现这样的结果:  
  thread  
  one.  
  two.  
   
  //MyThread.java  
  class   MyThread   extends   Thread  
  {  
                  public   static   void   main(String   []   args)  
                  {  
                                  MyThread   t=new   MyThread();  
                                  t.start();  
                                  try{  
                                  Thread.sleep(10);  
                                  }catch(Exception   e){}  
                                  System.out.println("one.");  
                                  t.start();  
                                  System.out.println("two.");  
                  }  
                  public   void   run()  
                  {  
                                  System.out.println("thread");  
                  }  
  }

NO.3   作者: xiazhihan

//MyThread.java  
  public   class   MyThread   extends   Thread{  
                  public   static   void   main(String   []   args){  
                        MyThread   t=new   MyThread();  
                  for(int   i=0;i<100;i++){  
                                  t.start();  
                                  System.out.println("one.");  
                                  if(!t.isAlive()){  
                                      t=new   MyThread();  
                                      t.start();  
                                  }  
                                  System.out.println("two.");  
                  }  
                  }  
                  public   void   run()  
                  {  
                                  System.out.println("thread");  
                  }  
  }  
   
   
  借用宝地,请大家帮我看看问题出在哪里?


    摘要: 我是新手,现在搞开发要用jbuilder,望各位大虾指点迷津。 ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE