Android 45: caused by terminating a thread
This is an old interview question. Usually, the interviewer will ask you about the Java thread and then ask this question again.
From theory to practice, this is a good way.
A thread is a way for the operating system to implement multiple tasks. It can be understood that a thread is the execution unit of a task. For example, in the Android system, each App has its own main thread, and you can also create a worker thread "Parallel" to work for us.
Method for creating a new thread in Java
Java provides language-level support for threads (relying on virtual machines ). The java. lang package contains the Thread class and Runnable interface, which can be used to create new threads for you.
1. extends Thread class
private class NumberCountThread extends Thread { public void run () { for (int i = 0;i<1000*1000;++i) { Log.d(TAG,count: +i); if(mThread.isInterrupted()) { Log.d(TAG,interrupted, return!); return; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2. implements Runnable interface
private class NumberCountRunnable implements Runnable { public void run() { int i = 0; while (true) { Log.d(TAG, count: + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i++; } } }
3. Start them
Thread mThread;//Thread mThread = new NumberCountThread(); mThread.start();//Runnable mThread = new Thread(new NumberCountRunnable()); mThread.start();
Java thread running status
1. New: Create a thread
2. Runnable: After the start method is called, the thread is ready to use, waiting for the CPU to be used.
3. Running: The thread is Running and the code in run is executed.
4. Blocked: the blocking status is divided into several situations: calling the wait method; the thread is obtaining the synchronization lock of the object; calling the sleep or join method; and sending an IO request.
5. Dead: After the thread is executed, or the run method is exited for other reasons, the life cycle ends.
Terminate a thread
Depending on the Dead status of the thread, the thread will automatically end its life after execution. This leads to common method 1, thread mark.
In the above two examples, we place a boolean mark in the loop body. When its value is false, we can stop the loop to complete the thread execution.
For example:
private boolean mThreadFlag = true;private class NumberCountThread implements Runnable { public void run() { int i = 0; while (mThreadFlag) { Log.d(TAG, count: + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i++; } } }
In another case, the thread is only executing a single time-consuming task, and the method fails once. The stop method in the thread is not recommended. How can I terminate it?
Currently, the interrupt method is used to "interrupt" the operation and immediately exit the run method to enter the Dead state.
Synchronize with the UI thread
1. Handler + Message
Reference: Practical Android skills: Memory leakage caused by Handler usage
2. View. post Method
When the thread is used to read the image in asserts, post the image directly to the main thread.
try { //InputStream inputStream = getResources().openRawResource(R.id.xxx); InputStream inputStream = assetManager.open(android/xxx.png); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mMainLayout.post(new Runnable() { @Override public void run() { Log.d(TAG, set image); mMainLayout.setBackground(new BitmapDrawable(bitmap)); } }); inputStream.close();} catch (IOException e) { e.printStackTrace();}