简体   繁体   中英

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.

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

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.

The second println() call almost certainly happens while the rollDice() function still is sleeping in its first call to sleep(300) . (Ie, before anything has been added to the list.)

Johannes Kuhn suggested that you "Try to .join the thread." 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. The whole point of threads is that they can work concurrently with each other.

This makes sense:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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