CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2EE / EJB / JMS

请问applet中怎样处理线程

楼主Nickcour(根)2003-08-03 13:26:46 在 Java / J2EE / EJB / JMS 提问

1、我想实现在Applet中用Runnable建两个线程两个线程的功能都是无限循环地计数在Applet中显示那两个计数进线程当前的值我写了如下代码但是编译时无问题运行时就出错请各位大侠指点一二  
  源码:  
  import   java.awt.*;  
  import   java.applet.Applet;  
  /*  
      <APPLET  
              CODE=dbuffer.class  
              WIDTH=200  
              HEIGHT=200>  
      </APPLET>  
  */  
   
  class   t1   implements     Runnable  
  {  
  public   boolean   sto1=true;  
  public   long   cou1;  
  Thread   thread;  
  t1()  
  {  
  thread=new   Thread(this,"t1");  
  thread.start();  
  }  
  public   void   run()  
  {  
  while(sto1)  
  {  
  cou1++;  
  try{  
  Thread.sleep(100);  
          }  
          catch(InterruptedException   e){}  
  }  
  }  
  }  
  class   t2   implements   Runnable  
  {  
  public   boolean   sto2=true;  
  public   long   cou2;  
  Thread   thread;  
  t2()  
  {  
  thread=new   Thread(this,"t2");  
  thread.start();  
  }  
  public   void   run()  
  {  
  while(sto2)  
  {  
  cou2++;  
  try{  
  Thread.sleep(100);  
          }  
          catch(InterruptedException   e){}  
  }  
  }  
   
  }  
   
  public   class   dbuffer   extends   Applet   implements   Runnable  
  {  
  t1   nt1=null;  
  t2   nt2=null;  
  Thread   nt3=null;  
          public   void   init()    
          {    
         
            }  
   
          public   void   start()    
          {  
        t1   nt1=new   t1();  
        t2   nt2=new   t2();  
        Thread   nt3=new   Thread(this);  
        nt3.start();  
          }  
   
          public   void   stop()    
          {   }  
   
          public   void   run()    
          {  
          while(true)  
          {  
          nt1.sto1=false;  
          nt2.sto2=false;  
          repaint();  
  try{  
  Thread.sleep(100);  
          nt1.sto1=true;  
          nt2.sto2=true;  
          }  
          catch(InterruptedException   e){}  
          }  
           
          }  
   
          public   void   paint   (Graphics   g)    
          {  
          nt1.sto1=false;  
          nt2.sto2=false;  
           
          g.drawString(String.valueOf(nt1.cou1),10,10);  
          g.drawString(String.valueOf(nt2.cou2),40,10);  
          }  
         
  }  
  问题点数:0、回复次数:2Top

1 楼wdydt163(东东)回复于 2003-08-03 15:51:44 得分 0

出什么错了Top

2 楼chongchong2001(虫虫)回复于 2003-08-03 21:14:07 得分 0

呵呵  
  在那个版块说了  
  /*  
      <APPLET  
              CODE=dbuffer.class  
              WIDTH=200  
              HEIGHT=200>  
      </APPLET>  
  */  
   
  class   t1   implements     Runnable  
  {  
  public   boolean   sto1=true;  
  public   long   cou1;  
  Graphics   g1   =   null;  
  Thread   thread;  
  t1(   Graphics   g   )  
  {  
  thread=new   Thread(this,"t1");  
  thread.start();  
  g1   =   g;  
  g1.setXORMode(   Color.WHITE   );  
  }  
  public   void   run()  
  {  
  while(sto1)  
  {  
  cou1++;  
  try{  
  Thread.sleep(100);  
  if(cou1>1)   g1.drawString(Long.toString(cou1-1),10,10);  
  g1.drawString(Long.toString(cou1),10,10);  
          }  
          catch(InterruptedException   e){}  
  }  
  }  
  }  
   
  class   t2   implements   Runnable  
  {  
  public   boolean   sto2=true;  
  public   long   cou2;  
  Graphics   g2   =   null;  
  Thread   thread;  
  t2(   Graphics   g   )  
  {  
  thread=new   Thread(this,"t2");  
  thread.start();  
  g2   =   g;  
  g2.setXORMode(   Color.WHITE   );  
  }  
  public   void   run()  
  {  
  while(sto2)  
  {  
  cou2++;  
  try{  
  Thread.sleep(100);  
  if(cou2>1)   g2.drawString(Long.toString(cou2-1),40,10);  
  g2.drawString(   Long.toString(cou2),40,10);  
          }  
          catch(InterruptedException   e){}  
  }  
  }  
   
  }  
   
  public   class   dbuffer   extends   Applet  
  {  
  t1   nt1=null;  
  t2   nt2=null;  
  Thread   nt3=null;  
   
          public   void   start()    
          {  
        t1   nt1=new   t1(   this.getGraphics()   );  
        t2   nt2=new   t2(   this.getGraphics()   );  
          }  
     
  }Top

相关问题

  • 怎样提高多线程的处理速度
  • 怎样在一个线程onterminate处理中,判断线程是否是被中断的?
  • 线程的消息环怎么做?怎样实现消息处理??
  • 怎样在线程外关闭线程?
  • 父线程怎样结束子线程?
  • 怎样能跟踪线程
  • 怎样调试多线程?
  • 怎样重启线程??
  • 怎样关闭线程?
  • 请问各位高手,在bcb里用线程拷贝大文件怎样处理好些!!

关键词

  • nt
  • 线程
  • sleep
  • start
  • null
  • cou
  • sto
  • thread
  • applet
  • interruptedexception

得分解答快速导航

  • 帖主:Nickcour

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo