你这涉及到并发访问的问题了。
你那个i是定义在外部的。
举个例子,一个线程访问了,然后输出,还未+1,这时候另一个线程也访问了,也输出,这时第一个线程+1,第二个线程+1,
输出的结果就应该是0  0   2   2
自然就不对了。
我没看明白你想要什么样的结果,你可以发你预想的结果我看下。
大体上理解的话你给i该应该加一把并发锁,当然这个锁好像只能加在方法上,那就把i定义为类的成员变量,然后提供get  set方法,对set方法加锁。

解决方案 »

  1.   

    说下思路吧:
    1.每个打印可是一个打印实例service,比如打印0的为class service0
    2.核心思想是,通过锁,具体是一个打印对象持有他前后两个打印对象的锁(用来控制顺序)
    3.这样不仅可以满足0~9 ,多少多可以循环交替打印
      

  2.   

    这个不行。 for(int i=0;i<10;i++){
        Thread th = new Thread(new Runnable(){//而且这样写也报错,可以写成new Thread(new Runnable(){,
          public void run(){
            System.out.println(i);// 这个i都会报错
          } 
        })
      }
      

  3.   

    for(int i=0;i<10;i++){
                final int f = i;//传值用。
                Thread th = new Thread(new Thread(){
                  public void run(){
                    System.out.println(f);//只能访问外部的final变量。
                  } 
                });
                th.start();
              }
      

  4.   

    for(int i=0;i<10;i++){
        final int f = i;
        Thread th = new Thread(new Thread(){
            public void run(){
                System.out.println(f);// 这个值要求要打印出0、1、2、3、4、5、6、7、8、9
            } 
        });
        th.start();
    }
      

  5.   

    菜鸟的笨办法写了一个:public class RotateThread
    {
    static int toBeOutput=0; //是否是要显示的线程计数。
    static Object o=new Object(); //同步对象锁
    public static void main(String[] args)
    {
    for(int i=0;i<10;i++)
    {
    final int j=i; //内部类只能接收final 类型。
    Thread th = new Thread(new Runnable()
    {
    public void run()
    {
    synchronized(o)
    {
    while(j!=toBeOutput)//不是应该输出线程,就等待。
    {
    try
    {
    o.wait();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }//end while.
    System.out.println(j);
    toBeOutput++;
    o.notifyAll(); //唤醒等待的线程。
    }//end synchronized. }//end run.
    });//end new Thread.
    th.start();
           }//end for.
    }//end main.
    }
      

  6.   

    不是很清楚你想要表达的意思,如果光是要输出结果
    public class MyThread extends Thread {
    public void run() {
    for (int i = 0; i < 10; i++) {
    System.out.println(i);
    }
    } public static void main(String[] args) throws InterruptedException {
     MyThread mt = new MyThread();
     MyThread mt1 = new MyThread();
     //你也可以改成for循环来创建线程
    //  mt.run();//一个线程先运行0,1,2..,0,1,2,3
    //  mt1.run();
     mt.start();//两个线程交替0,0,1,1,2,2结果可能会不同,因为两个线程交替,会出现不同情况
     mt1.start();
    }
    }
      

  7.   

    在 for的下一行也就是for循环里面的第一句加上:
    Thread.sleep(100);
      

  8.   

    你的需求真诡异,具体实现如下
    Thread t = null;
    for(int i = 0; i < 10; i++) {
    final int count = i;
    final Thread thread = t;
    t = new Thread() {
    @Override
    public void run() {
    System.out.println(count);
    if (thread != null)
    try {
    thread.join();
    } catch (Exception e) {
    }
    }
    };
    t.start();
    }
      

  9.   

    其实我的目的就是要用这种方法创建多线程来拷贝文件,所以不能用ynchronized和thread.sleep(),要不然的话就没有意义了
      

  10.   


       foreach(var data in datas)
                {
                    thread = new Thread(new ParameterizedThreadStart(obj =>
                    {
                          Console.WriteLine(obj);
                    }));                thread.Name = Guid.NewGuid().ToString();
                    thread.Start(data);
                }