0

I can't seem to find the answer to this question I'm having:

Is the method from the ACRA library ...

ACRA.getErrorReporter().putCustomData(Name, Content);

... thread safe?

I'd like to call it from two different threads and I'm not sure if i should or not.

I've searched through the documentation but I wasn't able to find anything related to this unfortunately, or maybe I'm just a bad Googleler :)

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

2 Answers2

1

If you're not sure, buy some insurance:

ErrorReporter er = ACRA.getErrorReporter();
synchronized( er ) {
    er.putCustomData( ..., .... );
}
323go
  • 14,143
  • 6
  • 33
  • 41
  • 1
    Not a bad idea ... If i won't be able to find the answer i'll definitely go this route. Thanks. (Btw ... that was funny :) ) – AndreiBogdan Mar 07 '13 at 16:36
  • Since ACRA is OSS, you could just peek in their code. And please post back what you learn, that'll save some time for future askers. – 323go Mar 07 '13 at 16:58
1

So I think I've figure it out.

Looking through the code I've noticed that the putCustomData method is in fact a call to a HashMap's method, more precisely put.

Upon further search I've found that the method put is not synchronized.

So the answer is no, calling putCustomData from two different threads can create problems for you.

For a solution to the problem: see 323go's idea below. In my case, I already had a singleton class which I used, so I just placed a synchronized method in that class in which I called the putCustomData method.

Note: If I am wrong, someone please let me know, but this is what I was able to find out.

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • 1
    HashMap is indeed not thread-safe. However, you could make it so by changing the assignment to a `ConcurrentHashMap`, or using `Collections.synchronizedMap( ... )`. Of course, that would require modification of your ACRA source, and a synchronized block (or method) within your singleton are less intrusive alternatives. – 323go Mar 07 '13 at 17:43