gpt4 book ai didi

Java 线程 : Multithreading - Race condition

转载 作者:行者123 更新时间:2023-11-30 08:36:27 25 4
gpt4 key购买 nike

我正在编写可以同时被多个用户访问的多线程程序,并且该程序必须避免竞争条件。

代码/多线程:

public class DataProcessor implements Serializable, Runnable {

private static final long serialVersionUID = 1L;

public DataProcessor() {

}

@Override
public void run() {
process();
}

private void process() {

int iSize = 5;

for (int iCounter = 0; iCounter < iSize; iCounter++) {
DataKey objDataKey = new DataKey();
ArrayList<String> list = //..fetch data method ()
HashMap<String, String> hmPQdata = //..fetch data method ()

SendNForgotHelperThread helperThread = new SendNForgotHelperThread(objDataKey, list, hmPQdata);
Thread t = new Thread(helperThread);
t.start();
}

}

class SendNForgotHelperThread implements Runnable {

private ArrayList<String> list;
private HashMap<String, String> hmPQdata;
private DataKey objDataKey;

public SendNForgotHelperThread(DataKey objDataKey, ArrayList<String> list, HashMap<String, String> hmPQdata) {
this.list = list;
this.hmPQdata = hmPQdata;
this.objDataKey = objDataKey;
}

@Override
public void run() {

try {

// Option 1 : synchronized method - SendNForgotHelperThread class object locking

DataCollector objDataSenderM = new DataCollector();
objDataSenderM.synchronizedMethodStore(this.objDataKey, this.list, this.hmPQdata);

// Option 2 : synchronized block - SendNForgotHelperThread class object locking

synchronized (this) {
DataCollector objDataSender = new DataCollector();
objDataSender.store(this.objDataKey, this.list, this.hmPQdata);
}

// Option 3 : Class level locking

synchronized (SendNForgotHelperThread.class) {
DataCollector objDataSender = new DataCollector();
objDataSender.store(this.objDataKey, this.list, this.hmPQdata);
}

} catch (Exception iex) {
System.out.println("Exception in thread: " + iex.getMessage());
}
}
}

class DataCollector {

public void store(DataKey objDataKey, ArrayList<String> list, HashMap<String, String> hmPQdata) {

HashMap<String, String> retrivedValue = (HashMap<String, String>) MemCacheUtil
.retrieveFromMemCache(objDataKey.getKey());

retrivedValue.putAll(hmPQdata);

MemCacheUtil.addToMemCache(objDataKey.getKey(), retrivedValue, "expTime value");

// Sending data in queue
sendDataToQueue(objDataKey, list, hmPQdata);

}

synchronized void synchronizedMethodStore(DataKey objDataKey, ArrayList<String> list,
HashMap<String, String> hmPQdata) {
store(objDataKey, list, hmPQdata);

}

}

class DataKey {
private String key;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}
}

public void sendDataToQueue(DataKey objDataKey, ArrayList<String> list, HashMap<String, String> hmPQdata) {
// sending data to queue

}

用户 1:

public class UserA {

public static void main(String[] args) {
DataProcessor objDataProcessor = new DataProcessor();
Thread thProcessorThread = new Thread(objDataProcessor, "PROCESSOR");
thProcessorThread.start();
}

用户 2:

public class UserB {

public static void main(String[] args) {
DataProcessor objDataProcessor = new DataProcessor();
Thread thProcessorThread = new Thread(objDataProcessor, "PROCESSOR");
thProcessorThread.start();
}

用户 A 和 B 将同时调用 DataProcessor 线程。很明显,选项 1 和 2 将面临竞争条件,因为它们锁定该类的对象/自类对象锁定,选项 3 将提供类级别的锁定 - 如果多个用户将同时访问该程序,则选项 3 将拖慢应用程序和多线程的整个目的将继续折腾。

任何人都可以建议任何关于如何处理这种情况的阅读。

编辑:

任何人都可以帮助处理 SendNForgotHelperThread 线程对象上的竞争条件 - 这个线程是从循环中调用的,并且对于每个循环,新线程 SendNForgotHelperThread 正在启动。

最佳答案

您正在将 DataProcessor 的两个不同实例传递给类 UserAUserB 中的线程,如果您启动这些主要方法,它将正常运行。竞争条件不会出现在您的应用程序中。

要发生竞争条件,您必须传递共享对象,即多个线程对同一对象进行操作,并且共享对象应具有字段/属性以在多个线程之间共享

    DataProcessor objDataProcessor = new DataProcessor();
Thread thProcessorThread1 = new Thread(objDataProcessor, "PROCESSOR-1");
thProcessorThread1.start();
Thread thProcessorThread2 = new Thread(objDataProcessor, "PROCESSOR-2");
thProcessorThread2.start();

关于Java 线程 : Multithreading - Race condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763319/

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