答案 0 :(得分:0)
不,AMQP.BasicProperties.Builder不是线程安全的。 我使用以下类测试它:
package com.anarsoft.agent;
import java.util.concurrent.atomic.AtomicInteger;
import com.rabbitmq.client.AMQP;
public class TestRabbitMq extends Thread {
static final AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
static final AtomicInteger threadCount = new AtomicInteger();
public void run() {
builder.clusterId("444").build();
threadCount.decrementAndGet();
}
public static void main(String[] args) throws Exception
{
for( int i = 0 ; i < 8 ; i++)
{
TestRabbitMq testNumberFormat = new TestRabbitMq();
threadCount.addAndGet(1);
testNumberFormat.start();
}
while(threadCount.get() > 0)
{
Thread.sleep(1000);
}
}
}
http://vmlens.com然后告诉我,在没有同步的情况下访问字段。由http://vmlens.com生成的跟踪:
com/rabbitmq/client/AMQP$BasicProperties$Builder.clusterId@28470003 (8,8)
Thread-0 (9)
Thread-1 (10) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-2 (11) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-3 (12) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-4 (13) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-5 (14) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-6 (15) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
Thread-7 (16) com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
com.anarsoft.agent.TestRabbitMq.run
你也可以看到它不是线程安全的来源:http://grepcode.com/file/repo1.maven.org/maven2/com.rabbitmq/amqp-client/2.6.1/com/rabbitmq/client/AMQP.java#AMQP.BasicProperties.Builder
public Builder clusterId(String clusterId)
{ this.clusterId = clusterId; return this; }