Objective
Recently, I've been preparing to summarize thread management on Android, so let's summarize the threads usage today.
Implement thread two Way 1.) Inherit the thread class
/** * Inherit thread mode */ privateclassextends thread { Syncthread (String name) { super(name); } @Override publicvoid run () { // Execute time-consuming operation } }
Example:
New Syncthread ("Thread One"new syncthread ("Thread Two"new syncthread ("thread Three"); Syncthread1.start (); Syncthread2.start (); Syncthread3.start ();
2.) Implement Runnable interface
/** * Implement Runnable mode */ privateclassimplements Runnable { @Override publicvoid run () { // Execute time-consuming operation } }
Example:
Newnew thread (syncrunnable, "thread One"new thread (syncrunnable, "thread Two" ) New Thread (syncrunnable, "thread three"); Syncthread1.start (); Syncthread2.start (); Syncthread3.start () ;
Thread Main function
run ()//contains code executed by thread runtime
start ()//For starting thread
sleep ()/sleep (long Millis)//thread hibernation, hand over the CPU, let the CPU to perform other tasks, and then thread into the blocking state, sleep method will not release the lock
yield ()//causes the current thread to hand over the CPU, allowing the CPU to perform other tasks, but not the thread going into the blocking state, but resetting it to the ready state, and the yield method will not release the lock
join ()/join (Long Millis)/join (Long millis,int nanoseconds)//waits for thread to terminate, and it is straightforward to say that the thread that originated the child thread waits for the child thread to run to the end before continuing to run down
Wait ()//hand over the CPU, allow the CPU to perform other tasks, put the thread into a blocking state, and release the lock
interrupt ()//interrupt thread, since the Stop function is obsolete, we use the interrupt method and the Isinterrupted () method to stop running threads, note that only the thread that is already blocked can be interrupted
getId ()//Gets the ID of the current thread
getName ()/setname ()//Get and set the name of the thread
getpriority ()/setpriority ()//Get and this is the priority of the thread the General property is represented by an integer of 1-10, the default priority is 5, the priority is the highest is 10, the high priority thread is executed with high probability
Setdaemon ()/isdaemo ()//Set up and determine if it is a daemon thread
CurrentThread ()//static function gets the current thread
Thread Threads Primary State
(1) new is in the new state once it is instantiated
(2) Runnable is in Runnable state after invoking the start function
(3) Running thread is in Running state after calling the run function by CPU execution
(4) Blocked Call join (), sleep (), wait () to put the thread in the Blocked state
(5) The Run () method of the Dead thread is completed or interrupted or exited abnormally, and the thread will reach the Dead state
How to stop a thread
With the list of functions above, I know that by using the interrupt method and the Isinterrupted () method to stop a running thread, you must first have the thread in a blocked state
/*** Destroying threading methods*/ Private voidDestroythread () {Try { if(NULL! = Thread && Thread.State.RUNNABLE = =thread. GetState ()) { Try{Thread.Sleep (500); Thread. Interrupt (); } Catch(Exception e) {e.printstacktrace (); } } } Catch(Exception e) {e.printstacktrace (); } finally{thread=NULL; } }
Thread Synchronization Issues
Thread synchronization is a problem that causes inconsistent data when multiple threads are accessing a data object.
1.) Examples: For example, multithreading operations a global variable
Private intCount = 100; Private BooleanIsRunning =false; Private voidtest1 () {isrunning=true; Syncthread SyncThread1=NewSyncthread ("Thread One"); Syncthread syncThread2=NewSyncthread ("Thread Two"); Syncthread syncThread3=NewSyncthread ("Thread Three"); Syncthread1.start (); Syncthread2.start (); Syncthread3.start (); } /*** Inherit thread mode*/ Private classSyncthreadextendsThread {syncthread (String name) {Super(name); } @Override Public voidrun () { while(isrunning) {count (); } } }
The Count function is not synchronized
Private void count () { if (Count > 0) { + "--->" + count--); Else { false; } }
Execution results: Careful observation reveals a phenomenon of data confusion
Add the Count function for synchronization
Private void count () { synchronized (this) { if (Count > 0) {
+ "--->" + count--); Else { false;}} }
Execution results
2.) Several ways of thread synchronization
Also, take the above as an example.
(1) Synchronization function
Private synchronized void count () { if (Count > 0) { + "--->" + count--); Else { false; } }
(2) Synchronizing code blocks
Private void count () { synchronized (this) { if (Count > 0) {
+ "--->" + count--); Else { false;}} }
(3) Implementing thread synchronization with special domain variables (volatile)
Private volatile int count = 1000;
The A.volatile keyword provides a lock-free mechanism for accessing domain variables.
B. Using a volatile modifier domain is equivalent to telling a virtual machine that the domain might be updated by another thread.
C. Therefore, each time the domain is used, it is recalculated instead of using the value in the Register
D.volatile does not provide any atomic manipulation, nor can it be used to modify a final type of variable
(4) thread synchronization with a re-entry lock
Reentrantlock (): Create an Reentrantlock instance
Lock (): Get lock
Unlock (): Release lock
Private void count () { lock.lock (); if (Count > 0) { + "--->" + count--); Else { false; } Lock.unlock (); }
Thread Usage for Android threading Management (i)