有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这些线程返回不正确的计算?

我对并发编程非常陌生,到目前为止我很喜欢:)!然而,我刚刚意识到并发编程是多么棘手

我有多个线程执行自己的计算。每个线程对某个变量进行操作并返回一个结果,但返回的结果不正确

此类执行线程计算:

public class SharedDataThread extends Thread {

private SharedData mySharedData;
private String myThreadName;
private static long testVariable = 0;

// Setup the thread

SharedDataThread(String name, SharedData sharedstuff) {
    super(name);
    mySharedData = sharedstuff;
    myThreadName = name;
}


public void run() {

    System.out.println(myThreadName + " is running");
    Thread me = Thread.currentThread(); // get a ref to the current thread

    if (me.getName() == "myThread1") {
        try {
            sleep(2000);

            mySharedData.acquireLock();
            System.out.println(me.getName()
                    + " is performing computations!");

            testVariable = testVariable + 20;
            testVariable = testVariable * 5;
            testVariable = testVariable / 3;

            System.out.println(me.getName() + " modified the value to : "
                    + testVariable + "\n");

            sleep(2000);
            mySharedData.releaseLock();
        } catch (InterruptedException e) {
            System.err.println("Failed to get lock when reading:" + e);
        }
    } else if (me.getName() == "myThread2") {
        try {
            sleep(2000);

            mySharedData.acquireLock();
            System.out.println(myThreadName
                    + " is performing computations!");

            testVariable = testVariable - 5;
            testVariable = testVariable * 10;
            testVariable = (long) (testVariable / 2.5);

            System.out.println(me.getName() + " modified the value to : "
                    + testVariable + "\n");

            sleep(2000);
            mySharedData.releaseLock();
        } catch (InterruptedException e) {
            System.err.println("Failed to get lock when reading:" + e);
        }
    } else if (me.getName() == "myThread3") {
        try {
            sleep(2000);

            mySharedData.acquireLock();

            System.out.println(me.getName()
                    + " is performing computations!");

            testVariable = testVariable - 50;
            testVariable = testVariable / 2;
            testVariable = testVariable * 33;

            System.out.println(me.getName() + " modified the value to : "
                    + testVariable + "\n");

            sleep(2000);
            mySharedData.releaseLock();
        } catch (InterruptedException e) {
            System.err.println("Failed to get lock when reading:" + e);
        }
    } else {
        try {
            sleep(2000);

            mySharedData.acquireLock();
            System.out.println(me.getName()
                    + " is performing computations!");

            testVariable = testVariable * 20;
            testVariable = testVariable / 10;
            testVariable = testVariable - 1;

            System.out.println(me.getName() + " modified the value to : "
                    + testVariable + "\n");

            sleep(2000);

            mySharedData.releaseLock();
        } catch (InterruptedException e) {
            System.err.println("Failed to get lock when reading:" + e);
        }
    }
    System.out.println("The final result of the variable is "
            + testVariable);
}

}

线程在另一个类中执行,该类具有自己的主执行线程:

public class SharingExample {

公共静态void main(字符串[]args){

SharedData mySharedData = new SharedData();


SharedDataThread myThread1 = new SharedDataThread("myThread1", mySharedData);
SharedDataThread myThread2 = new SharedDataThread("myThread2", mySharedData);
SharedDataThread myThread3 = new SharedDataThread("myThread3", mySharedData);
SharedDataThread myThread4 = new SharedDataThread("myThread4", mySharedData);


// Now start the threads executing

myThread1.start();
myThread2.start();
myThread3.start();
myThread4.start();

} }

SharedData类只是一个用于实现锁等的类

public class SharedData {

  private boolean accessing=false; // true a thread has a lock, false otherwise
  private int threadsWaiting=0; // number of waiting writers

  // attempt to acquire a lock
  public synchronized void acquireLock() throws InterruptedException{
  Thread me = Thread.currentThread(); // get a ref to the current thread
  System.out.println(me.getName()+" is attempting to acquire a lock!");

  ++threadsWaiting;
    while (accessing) {  // while someone else is accessing or threadsWaiting > 0
      System.out.println(me.getName()+" waiting to get a lock as someone else is accessing...");
      //wait for the lock to be released - see releaseLock() below
     wait();
    }
    // nobody has got a lock so get one
    --threadsWaiting;
    accessing = true;
    System.out.println(me.getName()+" got a lock!"); 
  }

  // Releases a lock to when a thread is finished

  public synchronized void releaseLock() {
      //release the lock and tell everyone
      accessing = false;
      notifyAll();
      Thread me = Thread.currentThread(); // get a ref to the current thread
      System.out.println(me.getName()+" released a lock!");
  }

}

问题出在哪里


共 (1) 个答案

  1. # 1 楼答案

    您的“testVariable”应标记为“volatile”。有关更多信息,请参阅本主题:Volatile Vs Static in java