Start and end of java _ thread (for android), javaandroid

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.