Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java线程:多线程-竞争条件_Java_Multithreading_Locking_Threadpool_Semaphore - Fatal编程技术网

Java线程:多线程-竞争条件

Java线程:多线程-竞争条件,java,multithreading,locking,threadpool,semaphore,Java,Multithreading,Locking,Threadpool,Semaphore,我正在编写多线程程序,可以从多个用户同时访问,该程序必须避免竞争条件 代码/多线程: public class DataProcessor implements Serializable, Runnable { private static final long serialVersionUID = 1L; public DataProcessor() { } @Override public void run() { process(); } private void pro

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

代码/多线程:

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

}
public class UserA {

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

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

用户2:

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

}
public class UserA {

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

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

用户A和B将同时调用数据处理器线程。 很明显,选项1和2将面临竞争条件,因为它们是该类的锁定对象/自类对象锁定,选项3将提供类级锁定-如果多个用户将同时访问该程序,则选项3将降低应用程序的速度,并且多线程的整个用途将被搁置

请任何人就如何处理这种情况提出建议

编辑:

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

}
public class UserA {

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

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

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

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

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

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

请检查SendNForgotHelperThread线程的5个实例-这将有竞争条件,但不会,
process
方法为每个线程创建一个新的
SendNForgotHelperThread
实例,这两个objDataProcessor实例是独立的,您应该将同一对象共享给多个线程以获得竞争条件。然后,选项1或选项2将正确地避免竞争条件,而第三个选项应被删除,因为它是类级锁定,将减慢应用程序的速度?否。如果您使用的是IDE,请在代码中设置断点并进行调试,您将了解发生了什么