theJava
theJava

Reputation: 15044

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

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 ?

Upvotes: 10

Views: 11075

Answers (6)

dnuttle
dnuttle

Reputation: 3840

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.

Upvotes: 9

Ted Hopp
Ted Hopp

Reputation: 234857

Every piece of code in Java is executed on some thread. By default, there is a "main" thread that calls your main method. All code in your program executes on the main thread unless you create another thread and start it. Threads start when you explicitly call the Thread.start() method; they can also start implicitly when you call an API that indirectly calls Thread.start(). (API calls that start a thread are generally documented to do so.) When Thread.start() is called, it creates a new thread of execution and calls the Thread object's run() method. The thread exits when its run() method returns.

There are other ways to affect threads, but that's the basics. You can read more details in the Java concurrency tutorial.

Upvotes: 1

At any time you have "execution points" where the JVM is running your code stepping through methods and doing what your program tells it to do.

For simple programs you only have one. For more complex programs you can have several, usually invoked with a new Thread().run or an Executor.

"Thread-safe" refers to that your code is written in such a way that one execution point cannot change what another execution point sees. This is usually very desirable as these changes can be very hard to debug, but as you only have one, there is not another so this does not apply.

Threads is an advanced subject which you will come back to later, but for now just think that if you do not do anything special with Threads or Swing this will not apply to you. It will later, but not now.

Upvotes: 3

Janick Bernet
Janick Bernet

Reputation: 21204

A class is thread safe when an object of that class can be accessed in parallel from multiple threads (and hence from multiple CPUs) without any of the guarantees that it would provide in a single threaded way to be broken.

You should read first about what exactly threads are, for instance on Wikipedia, which might make it then easier to understand the relation between classes and threads and the notion of threadsafety.

Upvotes: 1

Dmitry
Dmitry

Reputation: 17350

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.

Upvotes: 4

Cratylus
Cratylus

Reputation: 54094

Well, in your specific example, when your program runs, it has just 1 thread.
The main thread.

Upvotes: 1

Related Questions