java 线程安全在java中是什么意思或者我们什么时候称之为线程安全?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7301924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Thread-Safe mean in java or when do we call Thread-Safe?
提问by theJava
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.
这是我的班级 SomeName。现在什么是线程。
- Do we call the class as a thread.
- Do we call this class as thread when some other object is trying to access its method or members?
- Do we call this class as thread when some other object is trying to access this object?
- What does it mean when we call something in java as thread-safe ?
- 我们是否将类称为线程。
- 当其他对象试图访问其方法或成员时,我们是否将此类称为线程?
- 当其他对象试图访问这个对象时,我们是否将这个类称为线程?
- 当我们将 java 中的某些东西称为线程安全时,这意味着什么?
采纳答案by dnuttle
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.
正如其他人指出的那样,如果您的应用程序从不创建额外的线程,而只是从主方法运行,那么只有一个线程或一个“用餐哲学家”,因此不会发生线程锁。当您有多个线程时,避免线程锁的最简单方法是使用“监视器”,它只是一个被搁置的对象。实际上,您的方法必须在访问线程之前获得此监视器上的“锁定”,因此不会发生冲突。但是,您仍然可以使用线程锁,因为可能有两个对象试图访问两个不同的线程,每个线程都有自己的监视器。对象 A 必须等待对象 B 释放其在监视对象 1 上的锁;对象 B 必须等待对象 A 释放其在监视器对象 2 上的锁。所以现在您又回到线程锁了。
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 willrun 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.
Java 教程非常好地介绍了线程;这是我曾经需要的唯一资源。
回答by Dmitry
You might want to think of thread as CPU executing the code that you wrote.
您可能希望将线程视为执行您编写的代码的 CPU。
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.
线程安全类封装了任何需要的同步,因此客户端不需要提供自己的同步。
回答by Thorbj?rn Ravn Andersen
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.
在任何时候,您都有“执行点”,JVM 在其中运行您的代码,逐步执行方法并执行程序告诉它执行的操作。
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.
对于简单的程序,您只有一个。对于更复杂的程序,您可以有多个,通常使用 new Thread().run 或 Executor 调用。
"Thread-safe" refers to that your code is written in such a way that one execution point cannot change what anotherexecution 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.
Threads 是一门高级主题,您稍后会回来讨论,但现在只要想一下,如果您不对 Threads 或 Swing 做任何特别的事情,这将不适用于您。以后会,但不是现在。
回答by Ted Hopp
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.
Java 中的每一段代码都在某个线程上执行。默认情况下,有一个“主”线程调用您的main
方法。除非您创建另一个线程并启动它,否则程序中的所有代码都在主线程上执行。线程在您显式调用Thread.start()
方法时启动;当您调用间接调用的 API 时,它们也可以隐式启动Thread.start()
。(启动线程的 API 调用通常被记录为这样做。)当Thread.start()
被调用时,它会创建一个新的执行线程并调用 Thread 对象的run()
方法。线程在其run()
方法返回时退出。
There are other ways to affect threads, but that's the basics. You can read more details in the Java concurrency tutorial.
还有其他方法可以影响线程,但这是基础知识。您可以在Java 并发教程 中阅读更多详细信息。
回答by Janick Bernet
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.
当一个类的对象可以从多个线程(因此从多个 CPU)并行访问时,该类是线程安全的,而没有任何保证它会以单线程方式提供被破坏。
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.
您应该首先阅读有关线程的确切含义,例如在 Wikipedia 上,这可能会使您更容易理解类和线程之间的关系以及线程安全的概念。
回答by Cratylus
Well, in your specific example, when your program runs, it has just 1 thread.
The main thread.
好吧,在您的具体示例中,当您的程序运行时,它只有 1 个线程。
主线程。