0

标题几乎是不言自明的。

AMQP.BasicProperties.Builder线程安全吗?引文/来源?

我找不到任何表明这两种方式的东西......

4

1 回答 1

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; }
于 2013-10-27T15:12:34.510 回答