java - 线性java程序中的竞争条件

标签 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

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

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)

这是否意味着在具有控制台输出的简单程序中已经存在在 99.99% 的时间内表现确定的竞争条件?

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

或者 console.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();
}
}

最佳答案

e.printStackTrace将消息打印到 stderr (System.err),而所有其他输出都输出到 stdout (System.out)。默认情况下,standard output streams被重定向到同一目标(您的终端)。所以你的程序正在确定性地运行,它只是输出到两个 channel 。

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

将错误打印到标准输出
e.printStackTrace(System.out)

关于java - 线性java程序中的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330872/

相关文章:

java - 添加密码字段 - GUI

c++ - 我如何证明 volatile 分配不是原子的?

c++ - boost::进程间消息队列创建时的竞争条件?

c++ - 'undefined'怎么可能是竞争条件?

python - Django - 使用 F() 表达式但获取非原子更新

java - 为什么 hibernate 在最后添加到我的查询交叉连接

java - BufferedReader 第二次使用返回 null

java - 按照 Google App Engine 教程创建 Web 应用程序项目

concurrency - Enque 和 deque 使 channel 死锁

java - 创建线程来运行算法播放器