在Java中,如果两个类同时调用第三个类中的方法会发生什么?

发布于 2024-10-14 21:06:56 字数 167 浏览 6 评论 0原文

在我的项目中,我有一个由客户端类调用的游戏类。此时,游戏类将路径写入文件,客户端类将从该文件中读取并清除内容。我在这里遇到了太多的访问冲突,因此想使用另一个类作为存储路径数据的方法。但是,我想知道是否仍然存在问题,或者如果游戏类尝试调用存储类中的方法进行写入,而客户端类同时调用存储中的方法,结果会是什么课堂阅读和清晰。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小伙你站住 2024-10-21 21:06:57

在存在多个线程的情况下,您的类必须线程安全。实现此目的的一种方法是使并发访问的方法同步

下面是一个入门示例:

public class Test {

    public static void main(String[] args) throws Exception {
        new Thread() { public void run() { Test.method(); }}.start();
        new Thread() { public void run() { Test.method(); }}.start();
    }

    public synchronized static void method() {
        System.out.println("hello ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
        System.out.println("world");
    }
}

如果没有 synchronized 修饰符,该程序将打印

hello 
hello    
world
world

With the synchronized 关键字,只有一个线程在同一时间time 可以调用 method,因此程序打印

hello 
world
hello 
world

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:

public class Test {

    public static void main(String[] args) throws Exception {
        new Thread() { public void run() { Test.method(); }}.start();
        new Thread() { public void run() { Test.method(); }}.start();
    }

    public synchronized static void method() {
        System.out.println("hello ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
        System.out.println("world");
    }
}

Without the synchronized modifier, this program would have printed

hello 
hello    
world
world

With the synchronized keyword, only one thread at a time can call method, thus the program prints

hello 
world
hello 
world
热情消退 2024-10-21 21:06:57

我假设您的两个类在单独的线程中运行,因此它们可能会同时访问第三个类。对它们正在读写的资源的访问需要同步(互斥)。 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

夏末的微笑 2024-10-21 21:06:57

理论上正确的答案是:“任何事情都可能发生”。

这两个调用可以一个接一个地运行,也可以彼此交错运行,结果是不可预测的,并且可能是灾难性的。

这就是为什么 Java 为您提供了多种处理它的方法。

  1. 最简单(听起来)的方法是编写线程安全的方法。实际上,这通常意味着您应该只使用局部变量,并且不得修改作为参数传递给您的对象。 (没有副作用。)许多方法自动属于此类,在这种情况下您不必担心并发性。

  2. 如果您的方法无法实现线程安全,则需要以某种方式处理并发调用。 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.

  1. 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.

  2. 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, a volatile field or using Atomic* 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.

吻风 2024-10-21 21:06:56

听起来您需要考虑线程和同步。我建议阅读“Java 并发实践”。

Sounds like you need to think about threading and synchronization. I'd recommend reading "Java Concurrency in Practice".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文