资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

关于线程的一点疑问

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

程序1:(不使用线程)  
  import   java.awt.*;  
  public   class   graphtest     extends   Frame  
  {  
   
        public   static   void   main(String[]   args)  
        {  
  Frame   f=new   graphtest();  
  f.resize(300,300);  
  f.setBackground(Color.cyan);  
  f.show();  
        }  
        public   void   paint(Graphics   g)  
        {  
        g.setColor(Color.black);  
        drawcircle   t=new   drawcircle(g);  
        t.draw();  
        }  
   
  }  
  class   drawcircle  
  {  
  Graphics   bg;  
  public   drawcircle(Graphics   g)  
  {  
  bg=g;  
  }  
  public   void   draw()  
  {  
  bg.drawArc(50,50,100,100,0,360);  
  }  
  }  
  程序2:(使用线程)  
  import   java.awt.*;  
  public   class   graphtest2     extends   Frame  
  {  
   
        public   static   void   main(String[]   args)  
        {  
  Frame   f=new   graphtest2();  
  f.resize(300,300);  
  f.setBackground(Color.cyan);  
  f.show();  
        }  
        public   void   paint(Graphics   g)  
        {  
        g.setColor(Color.black);  
        drawcircle   t=new   drawcircle(g);  
        t.start();  
        }  
   
  }  
  class   drawcircle   extends   Thread  
  {  
  Graphics   bg;  
  public   drawcircle(Graphics   g)  
  {  
  bg=g;  
  }  
  public   void   run()  
  {  
  bg.drawArc(50,50,100,100,0,360);  
  }  
  }  
  程序1(不使用线程)可以画出一个圆,程序2(使用线程)画不出来,这是为什么?  
   
 

网友回答:

发表者:zcjl

修改代码如下,可以和原来一样  
   
                  public   void   paint(Graphics   g)   {  
  g.setColor(Color.black);  
  drawcircle   t   =   new   drawcircle(g);  
  t.start();  
  try   {  
  t.join();  
  }   catch   (InterruptedException   e)   {  
  e.printStackTrace();  
  }  
  }


 

 
热门推荐笔记本: 顶星笔记本
相关文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved