Start and end of java _ thread (for android), javaandroid
Package testJavaAll;
Import java. util. Timer;
Import java. util. TimerTask;
/**
* The test thread starts and stops the cancel
*
* October 21, 2014 10:34:57
*
* Yjbo
*/
Public class testThread {
Timer timer;
Private int x = 0;
Public testThread (int seconds, int seconds2 ){
Timer = new Timer ();
Timer. schedule (new RemindTask (), seconds * 1000, seconds2 * 1000 );
}
Class RemindTask extends TimerTask {
Public void run (){
X ++;
System. out. println ("---- xx ---" + x );
If (x <10 ){
// Enable the thread
System. out. println ("Time's up! ");
} Else if (x = 10 ){
// Stop the thread
Timer. cancel (); // Terminate the timer thread
System. out. println ("Time's up! ''' 000 ");
}
}
}
Public static void main (String args []) {
System. out. println ("About to schedule task .");
New testThread (1, 2 );
System. out. println ("Task scheduled .");
}
}
//}
Java (Android) thread Execution Problems
Actually, it can be judged.
Here is an example:
Public static void main (String [] s ){
Thread a = new Thread (){
@ Override
Public void run (){
While (! Interrupted ()){
NetOperation ();
}
}
};
A. start ();
If (! A. isInterrupted ()){
// Check whether the task is running
// If the execution has been completed, you can try again
System. out. println ("is run .");
}
}
Java or android asynchronous implementation can only use multithreading. Is there any other way?
In android, AsyncQueryHandler and AsyncTask are both used for asynchronous message transmission, but they are essentially multithreading. However, Android Can Help You encapsulate and conveniently call them. Hope to help you!