简体   繁体   中英

how to get variable value from a java thread

I have a thread which computes something and generates a 2 d array. I need to access the value of this 2 d array, when the thread is finished. How can I do that

        SwingWorker<Integer, Void> worker = new SwingWorker<Integer, Void>()
        {
            Object[][] valueMatrix = null;

            @Override
            public Integer doInBackground()
            {
                try
                {
                    valueMatrix = doChemicalSynonyms(termsArray_1, termsArray_2, mView.getSaveFilepath(), false, mView.getSheetName(), mView.getCategoryName(), mView.includePMIDs());
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    return -1;
                }
                return 0;
            }

            @Override
            public void done()
            {
                System.out.println(valueMatrix[0][0]);
            }
        };
        worker.execute();

doChemicalSynonyms functions generates the valueMatrix. How can I access it once the thread is complete, from the main class( something like global variable)

what you should do is to use the done() method to collect any result from the thread. Basically where you are now printing the first value, you can transfer it to another thread to use it there.

Beware that the done() is invoked in the EDT, so if you need to do an intensive operariton on it, another thread is necessary.

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