线性java程序中的竞争条件

0 java race-condition

附加的程序代码在大多数情况下产生以下输出:

6.0
8.0
10.0
12.0

java.lang.RuntimeException: dimensions not matching
    at hausaufgaben.linearAlgebra1.VectorRn.add(VectorRn.java:41)
    at hausaufgaben.linearAlgebra1.VectorRn.main(VectorRn.java:19)

3.0
6.0
9.0
12.0
Run Code Online (Sandbox Code Playgroud)

但在第二次执行时,它产生了以下输出

6.0
8.0
10.0
12.0

java.lang.RuntimeException: dimensions not matching
3.0
6.0
9.0
12.0

    at hausaufgaben.linearAlgebra1.VectorRn.add(VectorRn.java:41)
    at hausaufgaben.linearAlgebra1.VectorRn.main(VectorRn.java:19)
Run Code Online (Sandbox Code Playgroud)

这是否意味着在具有consoleoutput的简单程序中,已经存在在99.99%的时间内具有确定性行为的竞争条件?

或者是在一个单独的线程中执行的catch语句?

或者是控制台.output以一种奇怪的方式提出?

我很困惑这样的事情是如何发生的,即使它很少见.

package hausaufgaben.linearAlgebra1;

public class VectorRn {
/**
 * values that are the components
 */
private double[] values;

/**
 * @param args
 */
public static void main(String[] args) {
    VectorRn a = new VectorRn(1,2,3,4);
    VectorRn b = new VectorRn(5,6,7,8);
    VectorRn c = new VectorRn(1,2);
    double d = 3;
    System.out.println(a.add(b).toString());
    try {
        System.out.println(a.add(c).toString());
    }catch(Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
    System.out.println("\n"+a.mult(d).toString());
}    

/**
 * @param values copies the values passed to the constructor
 */
public VectorRn(double... values) {
    this.values = new double[values.length];
    System.arraycopy(values, 0, this.values, 0, values.length);
}

/**
 * @param v2 the vector added to a clone of this object
 * @return the sum of this Vector and v2
 */
public VectorRn add(VectorRn v2) {
    if(this.values.length != v2.values.length)
        throw new RuntimeException("dimensions not matching");
    VectorRn modifiedClone = new VectorRn(this.values);
    for(int i = 0; i < modifiedClone.values.length; i++){
        modifiedClone.values[i] += v2.values[i];
    }
    return modifiedClone;
}


/**
 * @param d the scalar by which the clone of this object will be multiplied
 * @return the d'th multiple of this Vector
 */
public VectorRn mult(double d) {
    VectorRn modifiedClone = new VectorRn(this.values);
    for(int i = 0; i < this.values.length; i++) {
        modifiedClone.values[i] *= d;
    }
    return modifiedClone;
}

/**
 * @return a string that contains a list of the components of the vector separated by newline characters
 * @see java.lang.Object#toString()
 */
public String toString() {
    StringBuilder tmp = new StringBuilder();
    for(double d : values) {
        tmp.append(d);
        tmp.append("\n");
    }
    return tmp.toString();
}
}
Run Code Online (Sandbox Code Playgroud)

phi*_*hag 5

e.printStackTrace将消息打印到stderr(System.err),而所有其他输出转到stdout(System.out).默认情况下,两个标准输出流都重定向到同一目标(您的终端).因此,您的程序确定性地运行,它只是输出到两个通道.

这种行为实际上是一个好主意; 程序可以重定向输出但仍然向用户显示错误.如果你不想要它,你可以用stdout打印错误

e.printStackTrace(System.out)
Run Code Online (Sandbox Code Playgroud)

  • +1.请注意,这实际上反映了并发性:在终端的某个层面,标准输出的写入和标准错误的写入是并行发生的. (2认同)