我现在又有一个问题,改进以下线程,使它能够在屏幕上循环打印0--9之间的数字呢?
    class MuilThread implements Runnable
{
public static void main(String[] args)
{           //static int i=0;
          MuilThread mt=new MuilThread();
          Thread t1=new Thread(mt);
          Thread t2=new Thread(mt);
         System.out.println("线程t1启动"); 
t1.start();
try
{
Thread.sleep(3000);
}
  catch(Exception e)
{
e.printStackTrace();}
System.out.println("线程t2启动");
t2.start();
  
}
public void run()
{int count=10;
for( int i=0;i<count;i++)
{
System.out.println(i);}
count=++count%10;
}
}

解决方案 »

  1.   

    class MuilThread implements Runnable
    {
    public static void main(String[] args)
    { //static int i=0;
    MuilThread mt=new MuilThread();
    Thread t1=new Thread(mt);
    Thread t2=new Thread(mt);
    System.out.println("线程t1启动"); 
    t1.start();
    try
    {
    Thread.sleep(3000);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    System.out.println("线程t2启动");
    t2.start();
    }
    public void run()
    {
    int i=0;
    while(true)
    {
    System.out.println(Math.abs(i%10));
    try{Thread.sleep(1000);}catch(Exception e){}
    i++;
    }
    }
    }
      

  2.   

    for( int i=0;i<count;i++)
    {
    System.out.println(i);}
    count=++count%10;貌似你的括号括错地方了。for( int i=0;i<count;i++)
    {
    System.out.println(i);count=++count%10;
    }
    这样应该没问题了。
      

  3.   

    谢谢,关于hoverlees的代码.我测试结果并没有循环打印0--9之间的数字,是否是要用到线程的同步的相关知识.