Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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_Thread Safety - Fatal编程技术网

Java 无法证明同时递增/递减

Java 无法证明同时递增/递减,java,multithreading,thread-safety,Java,Multithreading,Thread Safety,本周我从Java线程和并发开始;我需要一些关于下一个代码的帮助,我使用Thread而不是Runnable: 课程 package hilos; public class Hilo extends Thread { //Atributos public static int concurso = 0; private int veces; private boolean incrementoDecremento; //Método constructor public Hilo(bool

本周我从Java线程和并发开始;我需要一些关于下一个代码的帮助,我使用
Thread
而不是
Runnable

课程

package hilos;

public class Hilo extends Thread {

//Atributos

public static int concurso = 0;
private int veces;
private boolean incrementoDecremento;

//Método constructor

public Hilo(boolean inc, int numeroVeces){

    this.incrementoDecremento = inc;
    this.veces = numeroVeces;
}

//Método run

@Override

public void run(){

    for(int i = 0; i < this.veces; i++){    

        if(this.incrementoDecremento == true){

            concurso++;
            System.out.println("La variable introducida es: " + concurso);

        }else{

            concurso--;
            System.out.println("La variable introducida es: " + concurso);
        }
    }
}
}
我的目标是,如果我有两个任务使用相同的值,两个任务都开始随机递增和递减,因此基本上,它将产生一个由变量
prueba
确定的随机值,该变量位于
Main
类中


问题是出于某种原因,我不断地测试,最终结果总是零。我用
synchronized
语句和
Runnable
实现这一点没有问题,但我不可能使用
线程

不要使用静态int。使用原子整数。它正是为这种情况而设计的


如果您不想使用它,请将
concurso
设置为volatile
。您将看到缓存的值。

请尝试大于5的数字。您的线程可能运行得太快,以至于第一个线程在第二个线程开始之前就完成了

10000对我来说,这个问题很好:

public class BadThreads {
    public static void main(String[] args) {
        MyThread t1 = new MyThread( 10000);
        MyThread t2 = new MyThread(-10000);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }
        System.out.println(MyThread.shared);
    }

    private static class MyThread extends Thread {
        public static int shared;
        private int change;

        public MyThread(int change) {
            this.change = change;
        }

        public void run() {
            while (change < 0) {
                change++;
                shared--;
            }
            while (change > 0) {
                change--;
                shared++;
            }
        }
    }
}

…因此,我们成功地演示了并发性问题。你只跑了5次就得到了“幸运”--或者在你的例子中是不幸运的,因为你试图证明这个问题。:)

如果将变量递增5倍,递减5倍,则该值将与起始值(0)相同。在你的例子中,你看不到比赛条件的任何不良影响,所以它完全符合。如果您测试更大的计数或可能使用更大的线程,结果可能不是0,因为您没有使用原子操作。您有很多问题。有些严重,有些非常严重。首先,除非您实际扩展了
线程的功能,否则切勿
扩展线程
——请改用
可运行
。在非常严重的问题上——你有很多无记忆障碍的变量;这被称为竞争条件-您需要仔细阅读同步和易失性引用。@xFunkyTimes专门试图创建一个并发问题,很可能是出于演示目的(或者只是他们自己的理解)。但是,是的,原子整数是好的(如果使用正确的话)。在他使变量不稳定之后,他下一步应该怎么做才能真正解决这个问题呢?因为光有挥发性是不够的。
public class BadThreads {
    public static void main(String[] args) {
        MyThread t1 = new MyThread( 10000);
        MyThread t2 = new MyThread(-10000);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }
        System.out.println(MyThread.shared);
    }

    private static class MyThread extends Thread {
        public static int shared;
        private int change;

        public MyThread(int change) {
            this.change = change;
        }

        public void run() {
            while (change < 0) {
                change++;
                shared--;
            }
            while (change > 0) {
                change--;
                shared++;
            }
        }
    }
}
tmp$ javac BadThreads.java && java BadThreads
-8680