Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环的多线程同步_Java_Multithreading - Fatal编程技术网

Java 循环的多线程同步

Java 循环的多线程同步,java,multithreading,Java,Multithreading,我正在尝试编写一个运行两个线程的多线程程序。这两个线程一起执行for循环,并打印出数字0到99。我遇到的问题是,每个线程都运行for循环0到99,因此我得到0到99两次,而不是它们一起工作得到99 我的结果是这样的 1 1 2 2 3 3 我想要这样的东西,每条线打印一个数字,一直到100 1 2 3 4 all the way to 100 then stop 我做错了什么 这是我的密码 public class JavaApplication220 implements Runnable

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

我的结果是这样的

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);
                }
            }
        }
    }
}
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.AtomicInteger;
公开课考试{
公共静态void main(字符串参数[]){
AtomicInteger i=新的AtomicInteger(0);
i、 组(0);
TestingT thread1=新的TestingT(i);
TestingT thread2=新的TestingT(i);
ExecutorService executor=Executors.newCachedThreadPool();
执行人。执行(线程1);
执行人。执行(线程2);
executor.shutdown();
}
}
类TestingT实现了Runnable{
原子整数i;
测试(原子整数i){
这个。i=i;
}
@凌驾
公开募捐{
while(this.i.get()<100){
int i=this.i.incrementAndGet();
System.out.println(Thread.currentThread().getName()+“”+i);
}
}
}

如果需要订单(如中所述),这应该可以很好地满足您的要求

我想要这样的东西,每条线打印一个数字,一直到100。
1
2.
3.
4.
一直到100,然后停止

例如,这样做是有意义的(仅更改了JavaApplication220)

公共类JavaApplication220实现可运行{
公共静态INTI;
公共静态同步int GETNEXTANDPRINTIFSUPPORTED(int UPPERLIMIT4正在打印)
{
如果(++i
“我做错了什么?” 您希望使两个并行线程按顺序工作。并行编程是关于线程并行工作,而不是顺序工作。对于顺序执行,应该使用单线程。
我想这是一项学习任务。告诉老师线程是用于并行编程的,而不是顺序的。

您编写tme时的两个线程彼此完全独立,不进行通信。你为什么希望他们“分享”计票?是时候复习功课了。这个问题看起来像是家庭作业。这不是家庭作业。信不信由你。我知道如何在unix中使用c和互斥锁来实现这一点,但不知道如何在java中实现这一点。
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();
    }
}
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);
        }
    }
}
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);
                }
            }

        }
    }
}