Java multi-thread programming

Source: Internet
Author: User

Refer to Java API

Http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html

Java implements multi-threaded programming in two ways:

One is to inherit the Thread class

 class PrimeThread extends Thread {         long minPrime;         PrimeThread(long minPrime) {             this.minPrime = minPrime;         }          public void run() {             // compute primes larger than minPrime              . . .         }     } 

The usage is

PrimeThread p = new PrimeThread(143);     p.start(); 

Another method is to implement the runnable interface.

    class PrimeRun implements Runnable {         long minPrime;         PrimeRun(long minPrime) {             this.minPrime = minPrime;         }          public void run() {             // compute primes larger than minPrime              . . .         }     } 

The usage is

PrimeRun p = new PrimeRun(143);     new Thread(p).start(); 

On the Android platform, because activity has been integrated, you can only implement the interface.

The thread can be interpreted as a parallel processing program. On the one hand, it is displayed to the user interface and on the other hand, it uploads data to the server.

If you want to upload data at intervals, use

try {Thread.sleep(1000 * 10);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}

This is equivalent to uploading once every 10 seconds.

Today, we solved the logic of how to delete the same data. It took nearly one day.

 

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.