4

I'm using Google's Java API to work with Google Cloud Storage (GCS).

I have multiple worker threads that insert objects into GCS. Currently they share a common Storage instance, and use this to create Storage.Object.Insert instances:

synchronized(LOCK)
{
    insertObject = mStorage.objects().insert(mBucketName, objectMetadata, mediaContent);
}

They later call execute() on the Insert instance, uploading a file to GCS.

insertObject.execute();

My question is can I run the execute() call concurrently in different threads? The Insert object is unique to that thread, but the Storage object it was created with is shared between threads, so I'm worried this may cause problems.

Thanks in advance!

fejta
  • 3,061
  • 2
  • 17
  • 22
Felix
  • 3,783
  • 5
  • 34
  • 53
  • I would expect this to work just fine but I will see if I can find a more authoritative answer from someone who knows more about the java api client. – fejta May 18 '13 at 22:37
  • [This issue](https://code.google.com/p/google-api-java-client/issues/detail?id=144) leads me to believe that it's thread safe. – jterrace May 18 '13 at 22:56
  • Thanks for the comments guys, so it looks like it's pretty safe to assume it is thread safe. – Felix May 19 '13 at 20:58

1 Answers1

6

Yes! :)

That is, assuming the HttpTransport instance you are using is thread-safe, then having a shared Storage instance should be thread safe. What is not safe is to share the request class itself or its response across threads without additional locking that you provide. But as long as each thread is using its own request class, it is safe and in fact recommended to share the Storage and instance across threads.

Note: I am an owner of the google-api-java-client project

Yaniv Inbar
  • 1,409
  • 9
  • 10