4

I've been watching a lot of videos on data structures, and these terms are always being mentioned: synchronized/not synchronized and thread-safe/not thread-safe.

Can someone explain to me in simple words what synchronized and thread-safe mean in Java? What is sync and what is thread?

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
Michael
  • 313
  • 1
  • 3
  • 7
  • 3
    possible duplicate of [What does 'synchronized' mean?](http://stackoverflow.com/questions/1085709/what-does-synchronized-mean) – Erki M. Aug 23 '15 at 05:22
  • and http://stackoverflow.com/questions/2033879/what-does-threadsafe-mean – Erki M. Aug 23 '15 at 05:23
  • [Threads](http://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html) and [synchronization](http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html) explained in the Java Tutorials. – TNT Aug 23 '15 at 05:24
  • @ErkiM., but what is `multiple threaded environment`. Actually, what does `thread` mean in java? – Michael Aug 23 '15 at 05:24
  • @Michael http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html – Erki M. Aug 23 '15 at 05:25
  • 1
    This is _far_ too broad for SO's format. Plus, there are lots of resources out there that cover basic definitions like this. Where have you looked? What didn't you understand about what you read? You'll have to be much more specific, or else we won't know how to help you other than to regurgitate what's already out there and easily found. – yshavit Aug 23 '15 at 05:27

2 Answers2

4

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.

J.Selva
  • 171
  • 1
  • 14
0

As per CIP:

A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.

So thread safety is a desired behavior of the program in case it is accessed by multiple threads. Using the synchronized block is one way of achieving that behavior. You can also check the following:

What does 'synchronized' mean?

What does threadsafe mean?

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95