in Technology by

How to make a code snippet thread safe?

1 Answer

0 votes
by

Thread-safe code is code that remains correct when executed by multiple threads. Thus, No sequence of operations can violate the specification.

1 Invariants and conditions will hold during multithread execution without requiring additional synchronization by the client

2. The high level takeaway point is: thread-safe requires that the specification holds true during multithread execution. To actually code this, we have to do just one thing: regulate the access to mutable shared state

3. And there are three ways to do it: Prevent the access. Make the state immutable. Synchronize the access.

The first two are simple. The third one requires preventing the following thread-safety problems:

liveness deadlock: two threads block permanently waiting for each other to release a needed resource.

livelock: a thread is busy working but it’s unable to make any progress.

starvation: a thread is perpetually denied access to resources it needs in order to make progress.

safe publication: both the reference and the state of the published object must be made visible to other threads at the same time.

race conditions: A race condition is a defect where the output is dependent on the timing of uncontrollable events. In other words, a race condition happens when getting the right answer relies on lucky timing. Any compound operation can suffer a race condition, example: “check-then-act”, “put-if-absent”. An example problem would be if (counter) counter — ;, and one of several solutions would be

@synchronize(self)

{

if (counter) counter — ;

}.

Related questions

0 votes
    Should we make the resources thread safe explicitly if they are made to share across multiple clients?...
asked Jun 23, 2021 in Technology by JackTerrance
0 votes
    You are converting your HTML file into XHTML Strict. Which code snippet will validate without errors? asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    You are working with this XML code snippet from the XML document cars.xml. You need to return the information about the cars ... D.doc("cars.xml")/cars/car[integer(year) > 2000]...
asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code snippet? for i in [1, 2, 3, 4][::-1]: print (i) a) 4 3 2 1 b) error c) 1 2 3 4 d) none of the mentioned...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Predict the output of the following code snippet function foo(input: boolean) { let one = 1200; if (input) { let ans ... 1. 1201 2. Undefined 3. Compilation error 4. Runtime error...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    Which of these code snippet is the correct format to run SQL queries? 1. display(spark.sqlContext("select * from Fresco")) 2 ... ")) 4. show(spark.sql("select * from Fresco"))...
asked Oct 22, 2020 in Technology by JackTerrance
0 votes
    What is the code snippet to go back to a history twice? (a) history(2); (b) history(-2); ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Is the Session Factory Thread-safe in JBoss?...
asked Jun 5, 2021 in Technology by JackTerrance
0 votes
    Which class provides thread safe implementation of List? (a) ArrayList (b) CopyOnWriteArrayList (c) HashList (d) ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    PriorityQueue is thread safe. (a) True (b) False The question was asked during an interview. My question is ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    SessionFactory is a thread-safe object. (a) True (b) False The question was asked in final exam. The doubt ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I am using Eclipse with Pydev and googleAppengine. I have python 2.7 installed. I am trying to run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which class provides thread safe implementation of List? (a) ArrayList (b) CopyOnWriteArrayList (c) HashList ( ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
...