Research on Android multithreading (7) -- thread concurrency library in Java 5

Source: Internet
Author: User

Starting from this article, we will look at the new APIs for thread operations added to us after Java 5. First, we will look at the API documentation:


Java. util. concurrent contains many concurrent building blocks with good thread security, testing, and high performance. Let's take a look at the AtomicInteger under the atomic package.

Import java. util. concurrent. atomic. atomicInteger; public class AtomicIntegerTest {private static AtomicInteger data = new AtomicInteger (0); public static void main (String [] args) {new Thread (new Runnable () {@ Overridepublic void run () {data. incrementAndGet (); // Add }}). start (); new Thread (new Runnable () {@ Overridepublic void run () {data. decrementAndGet (); // subtract }}). start ();}}
The use of AtomicInteger can easily achieve data sharing between threads. If a member variable is to be operated by multiple threads, it can be processed using AtomicInteger. Other data types also have the corresponding Atomic.

Next let's take a look at the use of the thread concurrency pool. There are tool classes related to the thread concurrency pool in the java. util. concurrent package.

Import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. timeUnit; public class ThreadPoolTest {public static void main (String [] args) {// create three threads to ExecutorService threadPool = Executors. newFixedThreadPool (3); // ExecutorService threadPool = Executors. newCachedThreadPool (); // dynamically Add a thread // create a single thread (the thread will be restarted after death) // ExecutorService threadPool = Executors. newSingl EThreadExecutor (); // Add 10 tasks to the pool for (int I = 1; I <= 10; I ++) {final int task = imo-threadpool.exe cute (new Runnable () {@ Overridepublic void run () {// each task outputs 1 to 10for (int j = 1; j <= 10; j ++) {try {Thread. sleep (20);} catch (InterruptedException e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "loop of" + j + "for task" + task) ;}});} System. out. println ("10 task has commited"); thread Pool. shutdown (); // end the thread after the thread pool is executed // threadPool. shutdownNow (); End thread immediately // thread pool start timer Executors. newScheduledThreadPool (3 ). schedule (new Runnable () {@ Overridepublic void run () {System. out. println ("bombing! ") ;}}, 10, TimeUnit. SECONDS);/* Executors. newScheduledThreadPool (3 ). scheduleAtFixedRate (new Runnable () {@ Overridepublic void run () {System. out. println ("bombing! ") ;}}, 10, 3, TimeUnit. SECONDS );*/}}
There are three ways to create a thread pool:

1. newFixedThreadPool (n): Creates n threads.

2. newCachedThreadPool (): dynamically add threads (dynamically create threads based on the number of tasks in the task pool)

3. newSingleThreadPool (): creates a single thread (the thread will be re-created after its death)

In the above Code, three threads are created and 10 tasks are allocated. The three threads first execute the three tasks in the task pool, after a thread task is executed, it retrieves unexecuted tasks from the task pool and continues to execute until all the tasks in the task pool are completed, and the thread is in the waiting status, finally, use the shutdown () method to end the thread.




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.