Runnable & synchronizd and Lock
package com.example.hh.firstapp;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.locks.ReentrantLock;
public class MainActivity extends AppCompatActivity {
private Handler mHandler = new Handler();
private boolean flag;
private static final String test = "test";
private final ReentrantLock mLock = new ReentrantLock(true);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mState = start;
mStartTime = SystemClock.elapsedRealtime();
Log.d(TAG, "mStartTime: " + mStartTime);
mHandler.postDelayed(updateTime,100);
}
});
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flag = true;
// mLock.lock();
mThread.start();
if (mIsPause) {
mState = resume;
mIsResume = true;
mHandler.postDelayed(updateTime,100);
return;
}
mState = pause;
mIsPause = true;
mHandler.postDelayed(updateTime,100);
}
});
Button finish = findViewById(R.id.finish);
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mState = mFinish;
mHandler.postDelayed(updateTime,100);
synchronized (mLock) {
Log.d(TAG, “onClick: is notify”);
mLock.notify();
}
// mThread.notify();
}
});
/getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, new FirstAppFragment())
.commit();/
}
private Thread mThread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (mLock) {
Log.d(TAG, "thread: is notify");
if (flag) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, test, Toast.LENGTH_SHORT).show();
}
});
try {
// flag = false;
mLock.wait();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, “thread2: is notify”);
StringBuilder builder = new StringBuilder(“test”);
builder.append("—when flag is true");
// String newVal = builder.toString();
Toast.makeText(MainActivity.this, builder.toString(), Toast.LENGTH_SHORT).show();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
private static final String TAG = "";
private boolean mIsPause;
private boolean mIsResume;
private static final int start = 1;
private static final int pause = 2;
private static final int resume = 3;
private static final int mFinish = 4;
private int mState;
private long mStartTime;
private long mPauseTime;
private long mNextPauseTime;
private long mShowTime;
// private Thread mThread = new Thread(
Runnable updateTime = new Runnable() {
@Override
public void run() {
long nowTime = SystemClock.elapsedRealtime();
Log.d(TAG, "run: " + Thread.currentThread().getName());
switch (mState) {
case start:
Log.d(TAG, "run: " + mState);
mShowTime = nowTime - mStartTime;
break;
case pause:
Log.d(TAG, "run: " + mState);
mIsPause = true;
if (mIsResume) {
mNextPauseTime = nowTime - mStartTime - mPauseTime;
mIsResume = false;
return;
}
mPauseTime = nowTime - mShowTime - mStartTime - mNextPauseTime;
Log.d(TAG, "mPauseTime: " + mPauseTime);
break;
case resume:
Log.d(TAG, "run: " + mState);
mIsResume = true;
if (mIsPause) {
mPauseTime = mPauseTime + mNextPauseTime;
mIsPause = false;
}
mShowTime = nowTime - mPauseTime - mStartTime;
Log.d(TAG, "mShowTime: " + mShowTime);
break;
case mFinish:
Log.d(TAG, "run: " + mState);
break;
default:
break;
}
}
};
// private void toFragment() {
// startActionMode()
// }
}
同上,如果我们遇上需要线程暂时 wait 情况时,需要定义一个 Object 作为锁的存在,然后对需要暂停的线程或代码块,进行同步管理,即 synchronized ,若需要唤醒线程或相关代码块,即相关步骤可为 mLock.wait /mLock.sleep, mLock.notify