How do I call other methods of the Java runnable object?

I got some skeletons of classes I had to implement (I personally don't really agree with the program design, but I don't pity changing it: () and they work in this way:

I have a game class that implements runnable and represents a chess game

The server class will contain a list of multiple game classes it tracks

Well, that makes sense. Game implements runnable, so the server can put each game in its own thread

I'm a little confused about how Java threads work

All I know is the following:

Bind my runnable class to a thread and call After the start () method, the run () method of the Runnable class is called.

However, I have some other methods in the game class, such as:

capturePiece()

playerMakesMove()

wait

In the current design, the game operation is handled by the server When the player wants to capture a piece, the server will call game capturePiece().

In this case, does capturepiece () run on the game thread or the server thread? (caller's thread or callee's thread)

What would run () even do in this case?

Solution

Any method in any programming language executes in the same thread as the caller When you call thread When start (), it runs in the same thread that calls it

Now you know that the run () method of thread will not execute in the same thread as start But that's because start itself doesn't call run You will have to read more about threads to get a complete picture, but just imagine that start only creates a new thread (runnable) with some data structure. The newly created thread views the data structure, identifies the runnable and executes its running method

This is actually the only way to control the transfer from one thread to another: one thread generates some data, and another thread picks it up and processes it Control is not passed from one thread to another. It is communication and coordination between threads

If the game method is called by the server, the thread has nothing to do, right? On the contrary, if the server does not call the method directly, but represents the operation as data, game Run () can select an operation in its own thread and execute it

The only question now is where the server can place data for each game running in its own thread Run () knows to get data from it One option is to use BlockingQueue The server can put these action objects in the queue and the game thread can pick them up How do these two people know to use the same queue? There are many different methods. One is that the server creates a game with a queue and stores a map on its side As shown in the following skeleton:

class Server {
    Map<Game,BlockingQueue> games = ....;

    void createGame() {
        BlockingQueue queue = ....;
        Game game = new Game(queue);
        games.put(game,queue);
    }

    void foo() {
       Game game = ....; 
       Action action = ....; // identify the Game
       map.get(g).add(action);
    }
}

class Game {
    BlockingQueue _queue;

    Game(BlockingQueue queue) { 
         _queue = queue; 
    }

    void run() {
        while (true) {
            Action nextAction = _queue.take();
            // perform the action
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>