多线程同步循环

时间:2017-12-09 16:21:58

标签: java multithreading

我正在尝试编写一个运行两个线程的多线程程序。这两个线程一起执行一个for循环并打印出0到99的数字。我遇到的问题是每个线程运行for循环0到99,所以我得到0到99两次而不是它们一起工作到99。 / p>

我的结果看起来像这样

1
1
2
2
3
3

我想要这样的东西,每个线程一直打印一个数字到100。

1
2
3
4
all the way to 100 then stop

我做错了什么?

这是我的代码

public class JavaApplication220 implements Runnable {

    public int i;

    public void run() {
        synchronized (this) {
            for (i = 0; i < 100; i++) {
                System.out.println(i);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    Logger.getLogger(JavaApplication220.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

这是main()类

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NewClass {
    public static void main(String[] args) {
        JavaApplication220 thread1 = new JavaApplication220();
        JavaApplication220 thread2 = new JavaApplication220();

        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute(thread1);
        executor.execute(thread2);

        executor.shutdown();
    }
}

3 个答案:

答案 0 :(得分:1)

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Test {
    public static void main(String args[]){


        AtomicInteger i = new AtomicInteger(0);
        i.set(0);
        TestingT thread1 = new TestingT(i);
        TestingT thread2 = new TestingT(i);

        ExecutorService executor = Executors.newCachedThreadPool();
        executor.execute( thread1 );
        executor.execute( thread2 );

        executor.shutdown();
    }
}
class TestingT implements Runnable {
    AtomicInteger i;
    TestingT(AtomicInteger i ){
        this.i = i;
    }
    @Override
    public void run(){
        while(this.i.get() < 100) {
            int i = this.i.incrementAndGet();
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
    }
}

这应该可以满足您的要求

答案 1 :(得分:0)

如果需要订单,如

中所述
  

我想要这样的东西,每个线程一直打印一个数字到100。   1 2 3 4 all the way to 100 then stop

这样做是有意义的这样(只改变了JavaApplication220)

public class JavaApplication220  implements Runnable {
    public static int i;

    public static synchronized int getNextAndPrintIfSatisfied(int upperLimit4BeingPrinted)
    {
        if(++i < upperLimit4BeingPrinted)
        {
            System.out.println(i);
        }
        return i;
    }

    public void run(){
        {
            while(getNextAndPrintIfSatisfied(100) < 100)
            {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    Logger.getLogger(JavaApplication220.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
}

答案 2 :(得分:0)

&#34;我做错了什么?&#34; 您希望使两个并行线程按顺序工作。并行编程是关于并行工作的线程,而不是顺序编程。对于顺序执行,您应该使用单线程。 我想这是一项研究任务。告诉你的老师线程是并行编程,而不是顺序编程。