简体   繁体   English

从线程进行Java方法调用

[英]Java Method Call from Thread

如果我有一个游戏,每个客户端都有一个线程,维护有关该客户端的信息,而服务器有一个线程维护有关游戏世界的信息,则将从客户端线程之一调用主服务器线程上的方法来运行该线程,客户端线程还是服务器线程上的方法?

Threads are like the literal meaning of the word a string of commands. 线程就像一串命令一词的字面意思。 A computer has a single instruction pointer per thread to keep track where in the code the thread currently is. 一台计算机每个线程只有一个指令指针 ,以跟踪该线程当前在代码中的位置。 If you call a method, the program execution jumps there, and back once the method has finished. 如果调用方法,则程序执行将跳至该位置,并在该方法完成后返回。 But it's not going to leave the thread. 但这不会离开线程。 Code executing in a thread can never jump to a different thread. 在线程中执行的代码永远不能跳转到其他线程。

The only way you can have your code execute in a different thread is to make the other thread call it for you. 使代码在其他线程中执行的唯一方法是让另一个线程为您调用它。

But since you can't call the other thread directly, how can you make it call a method for you? 但是,由于您不能直接调用另一个线程,如何使它为您调用方法? Basically, program the other thread to wait for changes in a variable, and once it sees that variable change it can call the method. 基本上,对另一个线程进行编程以等待变量的更改,一旦看到该变量更改,就可以调用该方法。

So cross thread method invocation is actually communication through shared memory and it only works with special threads that are programmed to look at the shared memory. 因此,跨线程方法调用实际上是通过共享内存进行的通信,并且仅适用于已编程为查看共享内存的特殊线程。 You can't execute code in Threads that just blindly do their thing. 您不能在仅盲目的执行其任务的线程中执行代码。

To make it easy to program those things, we have BlockingQueue s in Java. 为了使这些事情易于编程,我们在Java中提供了BlockingQueue Any thread can put things in and other threads can wait for something to come out. 任何线程都可以放入东西,其他线程可以等待东西出来。 For example Runnable s that they execute. 例如,它们执行的Runnable

    final BlockingQueue<Runnable> codeQueue = new LinkedBlockingQueue<>();
    Thread serverThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (!Thread.interrupted()) {
                try {
                    Runnable code = codeQueue.take();
                    // call code in my context.
                    code.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    });
    Thread clientThread = new Thread(new Runnable() {
        @Override
        public void run() {
            codeQueue.add(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Hello from Server Thread.");
                }
            });
        }
    });

In this example, clientThread causes serverThread to print "Hello from Server Thread." 在这个例子中, clientThread导致serverThread打印“从服务器线程你好。” What serverThread does is also called an event loop. serverThread作用也称为事件循环。 Because it waits for events and reacts to them. 因为它等待事件并对它们做出反应。

Games typically have threads that do a (game) loop already. 游戏通常具有已经执行(游戏)循环的线程。 It's faily easy to add a line of code that checks for events and if it finds some to make it react to them. 添加一行代码来检查事件以及是否发现一些事件来对事件做出反应是很容易的。 Anything bigger will already have a way to invoke code in different threads. 任何更大的东西都已经可以在不同线程中调用代码。

would calling a method on the main server thread from one of the client threads run that method on the client thread, or the server thread? 从客户端线程之一在主服务器线程上调用方法将在客户端线程还是服务器线程上运行该方法?

In the client thread. 在客户端线程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM