Unable to modify the variable inside an Runnable from outside
<p>I am unable to change the value of <code>connection</code> variable from outside the class <code>ClientThread</code>. When the button is clicked, the <code>connection</code> value is changed to false but the code still runs.And moreover,i tried debugging but the works well perfectly that time.</p>

<pre><code>public class ClientActivity extends Activity implements OnClickListener{

    private EditText serverIp;

    private Button connectToServer;
    private Button disconnectFromServer;

    private String serverIpAddress = "";

    private boolean connected = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serverconnect);

        serverIp = (EditText) findViewById(R.id.et_serverip);
        connectToServer = (Button) findViewById(R.id.bt_connect);
        disconnectFromServer = (Button) findViewById(R.id.bt_disconnect);
        connectToServer.setOnClickListener(this);
        disconnectFromServer.setOnClickListener(this);
    }

        @Override
        public void onClick(View v) {
            if(v==connectToServer)
            {
                if (!connected) {
                    connected=true;
                    serverIpAddress = serverIp.getText().toString();
                    if (!serverIpAddress.equals("")) {
                        Thread cThread = new Thread(new ClientThread());
                        cThread.start();
                    }
                }

            }

            if(v==disconnectFromServer)
            {
                Toast.makeText(this,"Disconnecting from Server",Toast.LENGTH_LONG).show();
                connected=false;
            }
        }

    private class ClientThread implements Runnable {

        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                Log.d("ClientActivity", "C: Connecting...");
                Socket socket = new Socket(serverAddr, 8888);
                ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                int count=0;
                String message=null;
                Log.i("ClientActivity",(String)ois.readObject());
                do {
                    try {

                        message="Hey Server! "+count;
                        oos.writeObject(message);
                        count++;
                        if(connected==false)
                            break;

                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                        connected=false;
                    }
                }while(connected!=false);
                socket.close();
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected = false;
            }
        }
    }
}
</code></pre>
