Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thread safety is data sharing safety, And because in your code you make decisions based on the data your objects hold, the integrity and deterministic behaviour of it is vital. i.e</p> <p>Imagine we have a shared boolean instance variable across two threads that are about to execute a method with the following logic</p> <ul> <li>If flag is false, then I print "false" and then I set the flag back to true.</li> <li>If flag is true, then I print "true" and then I set the flag back to false.</li> </ul> <p>If you run continuously in a single thread loop, you will have a deterministic output which will look like:</p> <p>false - true - false - true - false - true - false ...</p> <p>But, if you ran the same code with two threads, then, the output of your output is not deterministic anymore, the reason is that the thread A can wake up, read the flag, see that is false, but before it can do anything, thread B wakes up and reads the flag, which is also false!! So both will print false... And this is only one problematic scenario I can think of... As you can see, this is bad.</p> <p>If you take out the updates of the equation the problem is gone, just because you are eliminating all the risks associated with data sync. that's why we say that immutable objects are thread safe.</p> <p>It is important to note though, that immutable objects are not always the solution, you may have a case of data that you need to share among different threads, in this cases there are many techniques that go beyond the plain synchronization and that can make a whole lot of difference in the performance of your application, but this is a complete different subject.</p> <p>Immutable objects are important to guarantee that the areas of the application that we are sure that don't need to be updated, are not updated, so we know for sure that we are not going to have multithreading issues</p> <p>You probably might be interested in taking a look at a couple of books:</p> <p>This is the most popular: <a href="http://www.amazon.co.uk/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/ref=sr_1_1?ie=UTF8&amp;qid=1329352696&amp;sr=8-1" rel="nofollow">http://www.amazon.co.uk/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/ref=sr_1_1?ie=UTF8&amp;qid=1329352696&amp;sr=8-1</a></p> <p>But I personally prefer this one: <a href="http://www.amazon.co.uk/Concurrency-State-Models-Java-Programs/dp/0470093552/ref=sr_1_3?ie=UTF8&amp;qid=1329352696&amp;sr=8-3" rel="nofollow">http://www.amazon.co.uk/Concurrency-State-Models-Java-Programs/dp/0470093552/ref=sr_1_3?ie=UTF8&amp;qid=1329352696&amp;sr=8-3</a></p> <p>Be aware that multithreading is probably the trickiest aspect of any application!</p>
    singulars
    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. This table or related slice is empty.
    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