snappy-java icon indicating copy to clipboard operation
snappy-java copied to clipboard

If Snappy-Java supports Thread-Safe?

Open Hesey opened this issue 14 years ago • 3 comments

  Recently I use Snappy-java for compressing bytes[] in Java. It's a multi-thread environment, I see your implementation is based on this code:
         int compressedByteSize = ((SnappyNativeAPI) impl).rawCompress(data, 0, byteSize, buf, 0);
  "impl" is a JNI implementation, I wonder that if it supports Thread-Safe or do you deal concurrent problem in JNI?
  Thanks.

Hesey avatar Oct 12 '12 02:10 Hesey

impl is a static field, and initialized in thread-safe manner. See also http://stackoverflow.com/questions/878577/are-java-static-initializers-thread-safe

xerial avatar Oct 12 '12 02:10 xerial

Yes I see that it initialized thread-safely. What I wanna know is when I use Snappy.compress(byte[]) by about 1000 threads in the same time ,will it represent correctly? Since it's a static method and static field, I worry about it. Thanks.

2012/10/12 Taro L. Saito [email protected]

impl is a static field, and initialized thread-safe manner. See also

http://stackoverflow.com/questions/878577/are-java-static-initializers-thread-safe

— Reply to this email directly or view it on GitHubhttps://github.com/xerial/snappy-java/issues/16#issuecomment-9365245.

Hesey avatar Oct 12 '12 03:10 Hesey

No shared state exists inside Snappy.compress code. You can used it safely from multiple threads.

xerial avatar Oct 12 '12 05:10 xerial