简体   繁体   English

将数据传递到另一个线程,java

[英]Pass data to another thread, java

I'm creating a small application learning about Java threading. 我正在创建一个有关Java线程的小型应用程序。 I want to have a thread running that will analyze a small piece of data (a poker hand), and output a display message when the hand is detected to be a winning hand. 我想运行一个线程,该线程将分析一小部分数据(扑克手),并在检测到该手为获胜手时输出显示消息。

I already have the part completed that generates hands until the deck is empty, I just need to figure out how to pass that data over into the other thread which analyzes and triggers the display message (just a simple System.out). 我已经完成了生成指针的部分,直到卡座为空为止,我只需要弄清楚如何将数据传递到另一个线程中,该线程就会分析并触发显示消息(只是一个简单的System.out)。

I'd like to do this to a currently running thread, instead of spawning a new thread for every hand that is dealt and passing the cards in the constructor. 我想对当前正在运行的线程执行此操作,而不是为发出的每手生成新线程并在构造函数中传递卡。

public static void main(String[] args) {
        Deck myDeck = new PokerDeck();
        DeckHandlerInterface deckHandler = new DeckHandler();

        (new Thread(new ThreadWin())).start();

        for(int x = 0; x < 2; x++) {
            while(myDeck.getDeck().size() >= deckHandler.getHandSize()) {
                deckHandler.dealHand(myDeck.getDeck());
            }
                    deckHandler.resetDeck();
        }

    }

My deckHandler returns a collection object which is what I want to pass to the other thread. 我的deckHandler返回一个收集对象,这是我想要传递给另一个线程的对象。 That's the part I'm not sure how to do. 那是我不确定该怎么做的部分。

You probably want to use a couple of BlockingQueue s. 您可能要使用几个BlockingQueue Have the thread that generates hands stick the hands in one queue. 让产生手的线程将手放在一个队列中。 The thread checking hands polls that queue and checks any hands it finds. 线程检查手轮询该队列并检查它发现的任何手。 Then it writes the results to a 2nd queue which the hand-generating thread can poll and display. 然后将结果写入第二个队列,手动生成的线程可以轮询并显示该队列。

There are many ways to accomplish this. 有很多方法可以完成此任务。

A simple approach might be to create a Queue that you pass in a reference to via the ThreadWin constructor. 一种简单的方法可能是创建一个通过ThreadWin构造函数传递引用的Queue
Then you just add the objects you wish to pass to the queue from the main thread, and listen for new objects on the queue in your ThreadWin thread. 然后,您只需将要从主线程传递到队列的对象添加到线程中,并在ThreadWin线程中ThreadWin队列中的新对象。 In particular it seems like a BlockingQueue might be a good fit here. 特别是在这里, BlockingQueue似乎很合适。

It sounds like you may want your "ThreadWin" to observe ( http://en.wikipedia.org/wiki/Observer_pattern ) the DeckHandler 听起来您可能希望您的“ ThreadWin”遵守( http://en.wikipedia.org/wiki/Observer_pattern)DeckHandler

Basically, the ThreadWin thread will "register" with the DeckHandler so it gets notified when the DeckHandler gets a new batch of PokerHands. 基本上,ThreadWin线程将在DeckHandler中“注册”,以便在DeckHandler获得新一批的PokerHands时得到通知。

When the ThreadWin thread is notified it will "stop resting" and determine which hand was best. 通知ThreadWin线程时,它将“停止休息”并确定哪只手最好。

You can use BlockingQueue to create simple consumer-producer scenario, there is even a simple example in the documentation. 您可以使用BlockingQueue创建简单的使用者-生产者方案,甚至在文档中也有一个简单的示例。

You should also read this to have a better understanding of concurrency. 您还应该阅读此书 ,以更好地了解并发性。

Propably the best method is to use java.util.concurrent package threadpool to execute tasks. 最好的方法可能是使用java.util.concurrent包threadpool执行任务。 Threadpool are nice, easy to implement, but you will not learn much apart from using the threadpools. 线程池非常好,易于实现,但是除了使用线程池之外,您不会学到很多东西。

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

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