简体   繁体   English

java中的protobuf是线程安全的吗?

[英]Is protobuf in java thread-safe?

I have the following protobuf msg defined:我定义了以下 protobuf msg:

message Counts {
    repeated int32 counts = 1;
}

which is shared between threads R and W as a builder:作为构建器在线程RW之间共享:

private final Counts.Builder countsBuilder;

Thread R will only read from countsBuilder and W will only write to countsBuilder .线程R只会从countsBuilder读取,而W只会写入countsBuilder The shared builder will be read, written-to and (at some point) built & sent over the network.共享构建器将被读取、写入和(在某个时候)构建和通过网络发送。

AFAIK, concurrent reads to messages are fine, but anything else must be synchronized at a higher level by the developer? AFAIK,对消息的并发读取很好,但是开发人员必须在更高级别同步其他任何内容吗? So, I can't actually write and read to the shared builder at the same time?所以,我实际上不能同时写入和读取共享构建器?

If this is not inherently thread-safe, I'm thinking of using some kind of thread-safe Collection<Integer> which I'll use for reading/writing and will (at some point) create a brand new message right before sending it over the network.如果这本质上不是线程安全的,我正在考虑使用某种线程安全的Collection<Integer> ,我将使用它进行读/写,并且(在某些时候)会在发送之前创建一个全新的消息通过网络。 Or am I missing something?或者我错过了什么?

Thanks!谢谢!

EDIT 1: I'm using protobuf 2.4.1 and java 6编辑 1:我使用 protobuf 2.4.1 和 java 6

EDIT 2: Some terminology and spelling fixes.编辑 2:一些术语和拼写修复。

You should be fine if you synchronize both your read and writes:如果您同步读取写入,您应该没问题:

synchronized (countsBuilder) {
   // modify countsBuilder
}

But remember that you also need to make sure that there aren't any race conditions when building the message;但是请记住,您还需要确保在构建消息时没有任何竞争条件; the writer thread is not allowed to make any writes after the message has been built.在构建消息后,不允许编写器线程进行任何写入。

according to https://developers.google.com/protocol-buffers/docs/reference/cpp It's not thread-safe in C++.根据https://developers.google.com/protocol-buffers/docs/reference/cpp在 C++ 中它不是线程安全的。

And Also not in java https://developers.google.com/protocol-buffers/docs/reference/java-generated而且也不是在 java https://developers.google.com/protocol-buffers/docs/reference/java-generated

I encountered this problem recently, And I found this.我最近遇到了这个问题,我发现了这个。 Here is what it said on C++这是它在 C++ 上所说的

A note on thread-safety:关于线程安全的说明:

Thread-safety in the Protocol Buffer library follows a simple rule: unless explicitly noted otherwise, it is always safe to use an object from multiple threads simultaneously as long as the object is declared const in all threads (or, it is only used in ways that would be allowed if it were declared const). Protocol Buffer 库中的线程安全遵循一个简单的规则:除非另有明确说明,同时使用来自多个线程的对象始终是安全的,只要该对象在所有线程中声明为 const(或者,它仅用于如果它被声明为 const,那将是允许的)。 However, if an object is accessed in one thread in a way that would not be allowed if it were const, then it is not safe to access that object in any other thread simultaneously.但是,如果一个对象在一个线程中以一种如果它是 const 则不允许的方式被访问,那么在任何其他线程中同时访问该对象是不安全的。

Put simply, read-only access to an object can happen in multiple threads simultaneously, but write access can only happen in a single thread at a time.简单地说,对一个对象的只读访问可以同时发生在多个线程中,而写访问一次只能发生在一个线程中。

Said on java:在java上说:

Note that builders are not thread-safe, so Java synchronization should be used whenever it is necessary for multiple different threads to be modifying the contents of a single builder.请注意,构建器不是线程安全的,因此当需要多个不同线程修改单个构建器的内容时​​,应使用 Java 同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM