What does thread-safe mean in java or when do we call thread-safe?

I do not understand this concept in any way.

public class SomeName { public static void main(String args[]) { } } 

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

  • We call the class a stream.
  • Do we call this class a thread when some other object tries to access its method or members?
  • We call this class a thread when some other object tries to access this object?
  • What does it mean when we call something in java as thread safe?
+8
java
source share
6 answers

Being thread safe means avoiding a few problems. The most common and probably the worst is called threadlock. An old analogy is the story of lunch philosophers. They are very polite and will never put out their chopsticks when someone else does the same. If they all stretch out at the same time, then they all stop at the same time and wait ... and nothing happens, because they are too polite to go first.

As someone else noted, if your application never creates additional threads, but simply starts from the main method, that is, there is only one thread or one “dining philosopher”, so the thread cannot happen. When you have multiple threads, the easiest way to avoid threadlock is to use a “monitor”, which is just an object that is set aside. In fact, your methods should get a “lock” on this monitor before accessing the threads, so there are no conflicts. However, you can still have threadlock, because there may be two objects trying to access two different threads, each with its own monitor. Object A must wait until object B releases its lock on monitor object 1; Object B must wait for Object A to release its lock on monitor object 2. So, now you are back to the streaming screen.

In short, thread safety is not very difficult to understand, but it takes time, practice and experience. The first time you write a multi-threaded application, you will come across threadlock. Then you will find out, and it will soon become quite intuitive. The biggest caveat is that you need to keep the multi-threaded part of the application as simple as possible. If you have a lot of threads, with lots of monitors and locks, it is exponentially harder to ensure that your dining philosophers never freeze.

The Java tutorial is very versed in streaming; it was the only resource i ever needed.

+9
source share

You might want to think about a thread as the processor executes the code you wrote.

What is a stream?

A thread is one consecutive control flow within a program.

From Java concurrency in practice :

Threaded classes encapsulate any necessary synchronization, so clients do not have to provide their own.

+4
source share

At any time, you have “execution points” in which the JVM runs your code, passing through the methods and doing what your program says.

For simple programs, you have only one. For more complex programs, you can have several, usually called using the new Thread (). Run or Executor.

“Thread-safe” means 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 since these changes can be very difficult to debug, but since you only have one, there is no other, so this does not apply.

Themes is an extended question that you will return to later, but for now, just think that if you don't do anything special with Threads or Swing, this will not apply to you. It will be later, but not now.

+3
source share

Well, in your specific example, when your program is running, it only has 1 thread.
Main stream.

+1
source share

A class is thread safe when an object of this class can be accessed in parallel from several threads (and, therefore, from several CPUs) without any guarantees that it would provide in a single stream method that would be violated.

First you should read about what streams are, for example on Wikipedia , which may simplify the understanding of the relationship between classes and streams and the concept of thread safety.

+1
source share

Each piece of code in Java runs on a thread. By default, there is a "main" thread that calls your main method. All the code in your program is executed in the main thread, unless you create another thread and start it. Themes begin when you explicitly call the Thread.start() method; they can also be run implicitly when you invoke an API that indirectly calls Thread.start() . (API calls that start a thread are usually documented for this.) When Thread.start() is Thread.start() , it creates a new thread of execution and calls the Thread object run() method. A thread exits when its run() method returns.

There are other ways to influence flows, but these are the basics. Read more in the Java concurrency tutorial .

+1
source share

All Articles