Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Thread-Safe mean in java or when do we call Thread-Safe?

Tags:

java

I am not understanding this concept in any manner.

public class SomeName {

     public static void main(String args[]) {

     }

}

This is my class SomeName. Now what is thread here.

  1. Do we call the class as a thread.
  2. Do we call this class as thread when some other object is trying to access its method or members?
  3. Do we call this class as thread when some other object is trying to access this object?
  4. What does it mean when we call something in java as thread-safe ?
like image 217
theJava Avatar asked Sep 04 '11 20:09

theJava


People also ask

What does it mean to be thread-safe Java?

thread-safety or thread-safe code in Java refers to code that can safely be utilized or shared in concurrent or multi-threading environment and they will behave as expected.

How can you tell if a thread is safe?

A method will be thread safe if it uses the synchronized keyword in its declaration.


2 Answers

Being thread-safe means avoiding several problems. The most common and probably the worst is called threadlock. The old analogy is the story of the dining philosophers. They are very polite and will never reach out their chopsticks to take food when someone else is doing the same. If they all reach out at the same time, then they all stop at the same time, and wait...and nothing ever happens, because they're all too polite to go first.

As someone else pointed out, if your app never creates additional threads, but merely runs from a main method, then there is only one thread, or one "dining philosopher," so threadlock can't occur. When you have multiple threads, the simplest way to avoid threadlock is to use a "monitor", which is just an object that's set aside. In effect, your methods have to obtain a "lock" on this monitor before accessing threads, so there are no collisions. However, you can still have threadlock, because there might be two objects trying to access two different threads, each with its own monitor. Object A has to wait for Object B to release its lock on monitor object 1; Object B has to wait for Object A to release its lock on monitor object 2. So now you're back to threadlock.

In short, thread safety is not terribly difficult to understand, but it does take time, practice and experience. The first time you write a multi-threaded app, you will run into threadlock. Then you will learn, and it soon becomes pretty intuitive. The biggest caveat is that you need to keep the multi-threaded parts of an app as simple as possible. If you have lots of threads, with lots of monitors and locks, it becomes exponentially more difficult to ensure that your dining philosophers never freeze.

The Java tutorial goes over threading extremely well; it was the only resource I ever needed.

like image 116
dnuttle Avatar answered Sep 28 '22 07:09

dnuttle


You might want to think of thread as CPU executing the code that you wrote.

What is thread?

A thread is a single sequential flow of control within a program.

From Java concurrency in practice:

Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own.

like image 42
Dmitry Avatar answered Sep 28 '22 08:09

Dmitry