線程開發中,遇到這個問題(這其實和android開發沒有關系,是有關線程的相關問題)大概代碼邏輯如下:
package com.example.myfirstapp2; import android.content.Intent; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.StrictMode; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.myfirstapp2.com.example.myfirstapp2.listener.SocketClientListener; import com.example.myfirstapp2.socketServer.SocketServerMain; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.HashMap; import java.util.Map; import static android.view.Window.FEATURE_ACTION_BAR; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; public static final String CHARGE_SUCCESS = "Charge successful!"; private Button button1 = null ; private Button button2 = null ; private Button button3 = null ; private Button button4 = null ; private Button button5 = null ; private Button buttonOtherFee = null ; private TextView viewBalanceLeftValue = null; private BufferedReader input = null; private PrintStream output = null; private Handler handler = null; private static boolean sendTag = false; private String sendStr = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = findViewById(R.id.feepolicy); viewBalanceLeftValue = findViewById(R.id.balanceleftvalue); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { sendStr = "hello world!"; Print("onClick" + sendStr); synchronized ((Object)sendTag) { sendTag = true; Print("notify" + sendStr); ((Object)sendTag).notify(); } } }); handler = new Handler(){ public void handleMessage(Message msg){ if(msg.what == 1 ) { char textBalanceLeftValue[] = ((Map<String,String>)msg.obj).get("key").toCharArray(); viewBalanceLeftValue.setText(textBalanceLeftValue,0,textBalanceLeftValue.length); } } }; Thread thread = new Thread(){ public void run(){ SocketClient socketClient = new SocketClient("10.0.2.2",8888); try { input = new BufferedReader(new InputStreamReader(socketClient.getSocket().getInputStream())); output = new PrintStream( socketClient.getSocket().getOutputStream() ); socketClient.setInput(input); socketClient.setOutput(output); } catch (IOException e) { e.printStackTrace(); } Map<String,String> mp = new HashMap<String,String>(); synchronized ((Object)sendTag){ while (true) { output.write(sendStr.getBytes(), 0, sendStr.getBytes().length); String result = socketClient.read(); mp.put("key", result); Message msg = handler.obtainMessage(); msg.what = 1; msg.obj = mp; handler.sendMessage(msg); try { ((Object)sendTag).wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }; thread.start(); } }
這里的鎖在中間會有值的變化,因此有可能在線程運行過程中就不是一個元素了,因此會報錯。把鎖的類型從boolean修改為Object,就ok了。