Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is tremendously <a href="http://en.wikipedia.org/wiki/Lock_%28computer_science%29#The_problems_with_locks" rel="nofollow">difficult</a> to write safe multi-threaded code that has any degree of complexity: this type of locking (using monitors) is fraught with all sorts of intermittent race conditions, deadlocks and livelock issues that often evades detection right up into promotion into production systems; if you can, consider using <a href="http://en.wikipedia.org/wiki/Message_passing" rel="nofollow">message passing</a>, <a href="http://en.wikipedia.org/wiki/Software_transactional_memory" rel="nofollow">software transactional memory</a> or <a href="http://en.wikipedia.org/wiki/Persistent_data_structure" rel="nofollow">persistent data structures</a> instead.</p> <p>FindBugs (or indeed any static analysis tools) can only go so far in detecting non-thread safe code: by their very definition, race conditions are time-sensitive - they require many execution runs to manifest, so static analysis fails in this respect because they don't run any code and only look for common code signatures. The best things IMHO to detect issues are:</p> <ul> <li><p>A second pair of eyes - rigorous code reviews with peers who are familiar with the code - goes along way in finding bugs that are not immediately obvious to the original author. </p></li> <li><p>Continuous integration &amp; exhaustive automated tests that exercise multi-threadedness on a variety of hardware, and <strong>ruthlessly</strong> investigate any 'intermittent' test failures. </p></li> </ul> <p>In answer to the second question, yes, all methods that make some reference to <code>terms</code> should be guarded by a synchronization monitor, regardless of whether it is a write or read operation; consider what happens if thread A calls <code>remove("BOB")</code> whilst thread B is calling <code>isExists("BOB")</code> when you don't have synchronization - thread A will be compacting the array list while thread B will be attempting to traversing it. </p> <p>At best, you will not be able to determine result of <code>isExists("BOB")</code>, but it is entirely possible that B could intermittently throw an <code>IndexOutOfBounds</code> exception since the size of the array could have changed (i.e shrunk) as it was being traversed over.</p> <p>Synchronize, and while you still can't be sure of the order in which the calls are made (due to the non-deterministic nature of scheduling), but at least you will be guaranteed that operations on <code>terms</code> will be atomic - that is, they are not being altered by something else whilst the current thread is running.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload