Java 将变量发送到线程中

Java 将变量发送到线程中,java,android,multithreading,Java,Android,Multithreading,我想在线程中使用一个数组,但我不知道把它放在哪里,以便新线程接受它并在run方法中使用它 到目前为止,我的代码是 public void game (Button[] temp) { Thread check = new Thread(new startGame()); check.start(); } class startGame implements Runnable { startGame() {}

我想在线程中使用一个数组,但我不知道把它放在哪里,以便新线程接受它并在run方法中使用它

到目前为止,我的代码是

    public void game (Button[] temp) {
        Thread check = new Thread(new startGame());
        check.start();
    }
    class startGame implements Runnable {
        startGame() {}
        public synchronized void run() {
            if (temp[0].getDrawingCacheBackgroundColor() == temp[1].getDrawingCacheBackgroundColor() )
            {
                temp[0].setVisibility(View.INVISIBLE);
                temp[1].setVisibility(View.INVISIBLE);
            } 
        }
    }

我很确定我必须将
temp
放入
startGame()
中,但是我应该将
temp
放在startGame类中的什么位置,这样我就可以在if语句中使用它了?

试着将它放在你的
startGame
类中,这个类应该命名为
startGame
。将其传递到构造函数中,并将类变量设置为值

 public void game (Button[] temp) {
        Thread check = new Thread(new startGame(temp));
        check.start();
    }
    class startGame implements Runnable {
        private Button[] temp
        startGame(Button[] temp) {
            this.temp=temp;
        }
        public synchronized void run() {
            if (temp[0].getDrawingCacheBackgroundColor() == temp[1].getDrawingCacheBackgroundColor() )
            {
                temp[0].setVisibility(View.INVISIBLE);
                temp[1].setVisibility(View.INVISIBLE);
            } 
        }
    }

乌格是正确的。将其设置为类变量,如果是共享的,则设置一些锁,并像访问任何其他类变量一样访问它


但正如Ashton Engberg在评论中敏锐地指出的那样,您不能从UI线程外部修改UI元素。

您不能从后台线程调用
setVisibility
(或做任何与UI相关的事情)。您需要将此作为可运行状态发布到主线程上的处理程序(
handler uiHandler=new handler(Looper.getMainLooper());uiHandler.post(runnable…);
)或在活动中使用
rununuithread(runnable)
。您实际想做什么