在Java中,如果两个类同时调用第三个类中的方法会发生什么?
在我的项目中,我有一个由客户端类调用的游戏类。此时,游戏类将路径写入文件,客户端类将从该文件中读取并清除内容。我在这里遇到了太多的访问冲突,因此想使用另一个类作为存储路径数据的方法。但是,我想知道是否仍然存在问题,或者如果游戏类尝试调用存储类中的方法进行写入,而客户端类同时调用存储中的方法,结果会是什么课堂阅读和清晰。
In my project I have a game class which is called by a client class. At the moment the game class writes a path to a file and the client class will read from this file and clear the contents. I am getting too many conflicts with access here so want to use another class as the method for storing the path data. I want to know, however, if there will still be a problem, or what the outcome will be if the game class tries to call the method in the storage class to write whilst the client class at the same instant calls the method in the storage class to read and clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在存在多个线程的情况下,您的类必须线程安全。实现此目的的一种方法是使并发访问的方法
同步
。下面是一个入门示例:
如果没有
synchronized
修饰符,该程序将打印With the
synchronized
关键字,只有一个线程在同一时间time 可以调用method
,因此程序打印In presence of multiple threads, your classes have to be thread safe. One way of achieving this is to make the concurrently accessed methods
synchronized
.Here is an example to get you started:
Without the
synchronized
modifier, this program would have printedWith the
synchronized
keyword, only one thread at a time can callmethod
, thus the program prints我假设您的两个类在单独的线程中运行,因此它们可能会同时访问第三个类。对它们正在读写的资源的访问需要同步(互斥)。 Java 有关键字“synchronized”,可以通过多种方式使用它来防止并发修改等,请参见 此处和此处欲了解详情
I'm assuming your two classes are running in separate threads, so they might access the third class at the same time. The access to the resource they are reading and writing needs to be synchronized (mutexed). Java has the keyword "synchronized", which can be used in multiple ways to prevent concurrent modifications and such, see here and here for details
理论上正确的答案是:“任何事情都可能发生”。
这两个调用可以一个接一个地运行,也可以彼此交错运行,结果是不可预测的,并且可能是灾难性的。
这就是为什么 Java 为您提供了多种处理它的方法。
最简单(听起来)的方法是编写线程安全的方法。实际上,这通常意味着您应该只使用局部变量,并且不得修改作为参数传递给您的对象。 (没有副作用。)许多方法自动属于此类,在这种情况下您不必担心并发性。
如果您的方法无法实现线程安全,则需要以某种方式处理并发调用。
synchronized
块是最常用的构造,但在某些情况下,易失性
字段或使用Atomic*
类就足够了。但这个主题太沉重,无法用一个答案来涵盖,所以我建议您阅读 并发教程。The theoretically correct answer is: "anything can happen".
The two calls can run one after the other or interleaved with each other, the results are unpredictable and potentially disastrous.
That's why Java offers you several ways of dealing with it.
The simplest (sounding) way is to write your methods threadsafe. In practice this usually means that you should only use local variables and must not modify the objects that are passed to you as parameters. (No side-effects.) Many methods automatically fall into this category, in which case you don't have to worry about concurrency.
If your method cannot be made threadsafe, you need to handle concurrent calls somehow.
synchronized
blocks are the most often used constructs but in some cases, avolatile
field or usingAtomic*
classes will suffice. But this subject is way too heavy to be covered in a single answer, so I suggest you read the concurrency tutorial.听起来您需要考虑线程和同步。我建议阅读“Java 并发实践”。
Sounds like you need to think about threading and synchronization. I'd recommend reading "Java Concurrency in Practice".