简体   繁体   English

Thread Run() 后实例变量为空 - Java 中的扑克骰子程序

[英]Instance variable is empty after Thread Run() - Poker Dice Program in Java

I am creating a poker dice game and I am having trouble obtaining the value of a List that is an instance variable.我正在创建一个扑克骰子游戏,但无法获取作为实例变量的 List 的值。

    public class ThrowDice { 
          List<String> result = new CopyOnWriteArrayList<>();

The value of this List is changed concurrently in a Thread run() method whose function is to set the five dice "rolling" at the same time.这个 List 的值在 Thread run() 方法中同时更改,其 function 是设置五个骰子同时“滚动”。 I am adding the resulting value (that is, the value of the die when it stops rolling) in the instance List with add(), however when I attempt to retrieve the value of the List later on, (for example, at the end of the rollDice method itself), the array is empty.我正在使用 add() 在实例 List 中添加结果值(即骰子停止滚动时的值),但是当我稍后尝试检索 List 的值时,(例如,在最后rollDice 方法本身),数组是空的。

The code:编码:

void rollDice(JLabel k, JLabel q, JLabel j, JLabel n, JLabel t, JLabel a) throws InterruptedException {

    new Thread() {

        @Override
        public void run() {
            String res = "";
            try {
                int times = 8;

                for (int i = 0; i <= times; i++) {

                    int random = (int) (Math.random() * 6) + 1;

                    switch (random) {
                        case 1:
                            k.setVisible(true);

                            res = "k";
                            break;
                        case 2:
                            q.setVisible(true);

                            res = "q";
                            break;
                        case 3:
                            j.setVisible(true);

                            res = "j";
                            break;
                        case 4:
                            t.setVisible(true);

                            res = "t";
                            break;
                        case 5:
                            n.setVisible(true);

                            res = "n";
                            break;
                        case 6:
                            a.setVisible(true);

                            res = "a";
                            break;
                    }

                    Thread.sleep(300);
                    if (i == times) {
                        result.add(res);
                    System.out.println("The result is " + result);// **this works, List has values**
                    } else {
                        setToFalse(k, q, j, t, a, n);

                    }

                } // end for

            } catch (InterruptedException e) {
            }

        }  //end run           

    }.start(); //end thread
 System.out.println("The result is " + result);// **--------------- List is empty (why?)**

}//end rolldice

} }

It is as if the values of the List get deleted after Run() ends, and I need to be able to retrieve the ArrayList value so that it can be passed to other methods.就好像 List 的值在 Run() 结束后被删除,我需要能够检索 ArrayList 值,以便可以将其传递给其他方法。

The second println() call almost certainly happens while the rollDice() function still is sleeping in its first call to sleep(300) .第二次println()调用几乎肯定会发生在rollDice() function 在第一次调用sleep(300)时仍处于休眠状态时。 (Ie, before anything has been added to the list.) (即,在将任何内容添加到列表之前。)

Johannes Kuhn suggested that you "Try to .join the thread." Johannes Kuhn 建议您“尝试加入.join ”。 What he meant was:他的意思是:

Thread t = new Thread() {

    @Override
    public void run() {
        ...
    }  //end run           
};

t.start();
t.join();    // This _waits_ until the thread has finished its work.
System.out.println(...);

But there's a problem with that suggestion: That is, it never makes any sense to .join() a thread in the very next statement after .start()ing it.但是这个建议有一个问题:也就是说,在 .start .start()ing ) 之后的下一条语句中.join()没有任何意义的。 The whole point of threads is that they can work concurrently with each other.线程的全部意义在于它们可以彼此同时工作。

This makes sense:这是有道理的:

t.start();
DoSomethingElseConcurrentlyWithThread_t(...);
t.join();

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

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