gpt4 book ai didi

java - Java中有多个线程访问同一方法时的线程安全性

转载 作者:行者123 更新时间:2023-12-03 13:19:03 25 4
gpt4 key购买 nike

我有这样的方法

private handleObj(Obj obj)
{
String data = obj.getData;
String key = obj.getKey;

store(key, data);
}

如果多个线程同时调用此方法,是否将在多个线程之间共享数据值?

就像线程A通过handleString(objFromThreadA)调用此方法一样,线程B通过handleString(objFromThreadB)调用此方法。是否有机会将线程A中的 String data的值替换为线程B中的 String data的值?

-----编辑1 -----

我对这里的线程安全有些困惑。下面的方法是我用于MQTT客户端的库中的回调。
/**
* This method is called when a message arrives from the server.
*
* <p>
* This method is invoked synchronously by the MQTT client. An
* acknowledgment is not sent back to the server until this
* method returns cleanly.</p>
* <p>
* If an implementation of this method throws an <code>Exception</code>, then the
* client will be shut down. When the client is next re-connected, any QoS
* 1 or 2 messages will be redelivered by the server.</p>
* <p>
* Any additional messages which arrive while an
* implementation of this method is running, will build up in memory, and
* will then back up on the network.</p>
* <p>
* If an application needs to persist data, then it
* should ensure the data is persisted prior to returning from this method, as
* after returning from this method, the message is considered to have been
* delivered, and will not be reproducible.</p>
* <p>
* It is possible to send a new message within an implementation of this callback
* (for example, a response to this message), but the implementation must not
* disconnect the client, as it will be impossible to send an acknowledgment for
* the message being processed, and a deadlock will occur.</p>
*
* @param topic name of the topic on the message was published to
* @param message the actual message.
* @throws Exception if a terminal error has occurred, and the client should be
* shut down.
*/
public void messageArrived(String topic, MqttMessage message) throws Exception;

每次调用此方法。我创建了一个新的线程来处理 message对象,这是我从 message对象中获取了很多数据和键。当我阅读评论时,它说此方法被同步调用,这是否意味着消息一定是线程安全的?

这是我处理消息对象的方式。
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
try
{
handleMessage(topic, new String(message.getPayload()));
}

catch (Exception e)
{
e.printStackTrace();
}
}
};

threadPool.execute(runnable);
}

在handleMessage(String topic,String message)中,我有这样的东西:
private void handleMessage(String topic, String message)
{
JSONObject messageJson = new JSONObject(message);

//get values from JSON
String value = messageJson.get("key");
}

因此,我不确定我在这里执行的操作是否是线程安全的。如果我将新分配的String( new String(message.getPayload()))传递给我的handleMessage方法,这是否意味着将新String放在一个线程的堆栈上,并且其他任何线程都不能共享该字符串?

最佳答案

每个方法调用都是其自身范围内的方法。

您不需要线程使其可见-只需使用递归方法即可使这一点变得清晰:

void middle (String s) {
println ("<" + s);
if (s.length () > 2)
middle (s.substring (1, s.length () - 1));
println (s + ">");
}

在第4行中,调用了该方法的新实例,并且s现在是外部s的截断版本。但是在完成内部调用之后,外部s根本不受影响。
-> middle ("foobar")
<foobar
<ooba
<ob
ob>
ooba>
foobar>

对于线程,可能的冲突(更好的是:不可能的冲突)只会更难以显示。

但是问题是合法的。乍一看,等效代码在bash中的表现如何(对我而言,非常令人惊讶):
middle () {
s=$1
echo "< $s"
len=${#s}
if (( $len > 2 ))
then
middle ${s:1:((len-2))}
fi
echo "$s >"
}

middle "foobar"

< foobar
< ooba
< ob
ob >
ob >
ob >

关于java - Java中有多个线程访问同一方法时的线程安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491226/

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