gpt4 book ai didi

java - 为什么这些线程返回错误的计算结果?

转载 作者:行者123 更新时间:2023-12-01 12:22:26 24 4
gpt4 key购买 nike

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

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

此类执行线程计算:

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 {

public static void main(String[] 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!");
}

}

问题出在哪里?

最佳答案

您的“testVariable”应标记为“ volatile ”。有关详细信息,请参阅此主题:Volatile Vs Static in java .

关于java - 为什么这些线程返回错误的计算结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26578037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com