"what do the terms synchronized and thread-safe mean? [closed]" Code Answer


a thread is an execution path of a program. a single threaded program will only have one thread and so this problem doesn't arise. virtually all gui programs have multiple execution path and hence threads - one for processing the display of the gui and handing user input, others for actually performing the operations of the program. this is so that the ui is still responsive while the program is working.

in the simplest of terms threadsafe means that it is safe to be accessed from multiple threads. when you are using multiple threads in a program and they are each attempting to access a common data structure or location in memory several bad things can happen. so, you add some extra code to prevent those bad things. for example, if two people were writing the same document at the same time, the second person to save will overwrite the work of the first person. to make it thread safe then, you have to force person 1 to wait for person 2 to complete their task before allowing person 1 to edit the document.

synchronized means that in a multiple threaded environment, a synchronizedobject does not let two threads access a method/block of code at the same time. this means that one thread can't be reading while another updates it.

the second thread will instead wait until the first is done. the overhead is speed, but the advantage is guaranteed consistency of data.

if your application is single threaded though, synchronized has no benefit.

By normany on September 10 2022

Answers related to “what do the terms synchronized and thread-safe mean? [closed]”

Only authorized users can answer the Search term. Please sign in first, or register a free account.