1.背景
processor处理器
task任务
Concurrency并发
one task in your program
If one task in your program is unable to continue because some condition outside of the control of the program
- Block阻塞
The other tasks in the program can continue to excute when one task is blocked.
分布式系统
- 并发编程
程序被分成多个独立的任务,每个任务由它的执行线程驱动,一个线程就是进程中一个单一的顺序流。(程序、任务、线程、进程的关系??)
1.并发的实现
是否使用多线程同时驱动任务就是实现了并发?
2.并发和顺序编程的区别
顺序编程:任意时刻只执行一个步骤
并发:某些时刻可以同时(因为时间短造成的同时,实际上还是有先后区分的?)执行多个任务
通过写一个下载图片的App检查对View和线程的理解
1. 子线程向主线程发送消息,更新UI
Android 编程:calledfromWrongThreadException 的原因
diff --git a/app/src/main/java/com/hmdglobal/app/camera/app/CameraAppUI.java b/app/src/main/java/com/hmdglobal/app/camera/app/CameraAppUI.java
index 7b0d05b..1376eaf 100644
--- a/app/src/main/java/com/hmdglobal/app/camera/app/CameraAppUI.java
+++ b/app/src/main/java/com/hmdglobal/app/camera/app/CameraAppUI.java
@@ -1034,6 +1034,10 @@
mIntentReviewControls = (IntentReviewControls) appRootView.findViewById(R.id.intent_review_controls);
mViewFinderLayout = (FrameLayout) mAppRootView.findViewById(R.id.view_finder_layout);
+
+ Looper looper = Looper.myLooper();
+ mMainHandler = new MainHandler(looper);
+
}
public boolean isScreenReversed() {
@@ -2869,6 +2873,29 @@
}
}
+ private MainHandler mMainHandler;
+ private class MainHandler extends Handler {
+ public MainHandler(Looper looper) {
+ super(looper);
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ String currSelect = (String)msg.obj;
+ ButtonManager buttonManager = mController.getButtonManager();
+ if(currSelect != "") {
+ buttonManager.hideButton(ButtonManager.BUTTON_FLASH);
+ buttonManager.hideButton(ButtonManager.BUTTON_HDR);
+ buttonManager.hideButton(ButtonManager.BUTTON_MOTION);
+
+ } else {
+ buttonManager.showButton(ButtonManager.BUTTON_FLASH);
+ buttonManager.showButton(ButtonManager.BUTTON_HDR);
+ buttonManager.showButton(ButtonManager.BUTTON_MOTION);
+ }
+ }
+ }
+
/**
* to the bottom bar mode options based on limitations from a
* {@link com.hmdglobal.app.camera.hardware.HardwareSpec}.
@@ -2894,6 +2921,9 @@
public void onItemClick(int pos, String path) {
SharedPreferences sharedPreferences = mController.getAndroidContext().getSharedPreferences("FirstRun",0);
boolean first_run = sharedPreferences.getBoolean("First",true);
+
+ Message message = Message.obtain();
+
if (first_run) {
sharedPreferences.edit().putBoolean("First", false).apply();
ToastUtil.showToast(mController.getAndroidContext(), R.string.firstboot_message, Toast.LENGTH_LONG);
@@ -2903,16 +2933,25 @@
@Override
public void run() {
if(TextUtils.isEmpty(mCurrSelect) || !path.equals(mCurrSelect)){
+
BeaurifyJniSdk.preViewInstance().nativeChangePackage(path);
mCurrSelect = path;
setEffectEnable(true);
+
}else{
+
setEffectEnable(false);
if(path.equals(mCurrSelect)){
BeaurifyJniSdk.preViewInstance().nativeDisablePackage();
mCurrSelect = "";
}
}
+
+ message.obj = mCurrSelect;
+ mMainHandler.sendMessage(message);
+
}
});
}
2.使用Handler在子线程更新设置
private Handler mMainHalder = new Handler();
mMainHalder.post(new Runnable() {
@Override
public void run() {
mFaceView.setBlockDraw(false);
mFaceView.setOriginalCameraBound(cameraBound);
mFaceView.setFaces(faces, extendedFaces);
}
});
3.UI线程更新ui
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
子线程和主线程的互相传递
if (synced) {
// Don't bother to wait since camera is in bad state.
Log.d(TAG,"valid" + getCameraState().isInvalid());
if (getCameraState().isInvalid()) {
return;
}
final WaitDoneBundle bundle = new WaitDoneBundle();
getDispatchThread().runJobSync(new Runnable() {
@Override
public void run() {
//发送RELEASE的消息给主线程,去release camera
getCameraHandler().obtainMessage(CameraActions.RELEASE).sendToTarget();
//发送消息给子线程去notify(如果因为message很多导致长期阻塞,就无法notify)
getCameraHandler().post(bundle.mUnlockRunnable);
}}, bundle.mWaitLock, CAMERA_OPERATION_TIMEOUT_MS, "camera release");
} else {
getDispatchThread().runJob(new Runnable() {
@Override
public void run() {
//防止消息队列消息过多,等待时间过长
getCameraHandler().removeCallbacksAndMessages(null);
getCameraHandler().obtainMessage(CameraActions.RELEASE).sendToTarget();
}});
}
4.CameraAgent.CameraProxy里的异步和同步
public void setPreviewDisplay(final SurfaceHolder surfaceHolder) {
try {
getDispatchThread().runJob(new Runnable() {
@Override
public void run() {
getCameraHandler()
.obtainMessage(CameraActions.SET_PREVIEW_DISPLAY_ASYNC, surfaceHolder)
.sendToTarget();
}
});
} catch (final RuntimeException ex) {
getAgent().getCameraExceptionHandler().onDispatchThreadException(ex);
}
}
public void setPreviewTextureSync(final SurfaceTexture surfaceTexture) {
// Don't bother to wait since camera is in bad state.
if (getCameraState().isInvalid()) {
return;
}
final WaitDoneBundle bundle = new WaitDoneBundle();
try {
getDispatchThread().runJobSync(new Runnable() {
@Override
public void run() {
getCameraHandler()
.obtainMessage(CameraActions.SET_PREVIEW_TEXTURE_ASYNC, surfaceTexture)
.sendToTarget();
getCameraHandler().post(bundle.mUnlockRunnable);
}
}, bundle.mWaitLock, CAMERA_OPERATION_TIMEOUT_MS, "set preview texture");
} catch (final RuntimeException ex) {
getAgent().getCameraExceptionHandler().onDispatchThreadException(ex);
}
}