Android multithreaded Research (7) thread concurrency library in--java5

Source: Internet
Author: User

From this beginning we will look at the new API for threading that we added after Java 5, first look at the API documentation:


Java.util.concurrent contains many concurrent building blocks that are thread-safe, well-tested and high-performance, let's look at the Atomicinteger under the atomic package first.

Import Java.util.concurrent.atomic.atomicinteger;public class Atomicintegertest {private static Atomicinteger data = New Atomicinteger (0);p ublic static void Main (string[] args) {new Thread (new Runnable () {@Overridepublic void run () {DATA.I Ncrementandget ();  +}). Start (); New Thread (New Runnable () {@Overridepublic void run () {data.decrementandget ();  Minus}). Start ();}}
With Atomicinteger, it is convenient to implement data sharing between threads, and if a member variable is to be manipulated by multiple threads, it can be handled using Atomicinteger, and other data types have corresponding atomic.

Let's take a look at the use of a thread concurrency pool, which has a related tool class for threading concurrency pools under 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 3 threads to perform a task Executorservice ThreadPool = Executors.newfixedthreadpool (3);//executorservice ThreadPool = Executors.newcachedthreadpool (); Dynamic add thread//create a single thread (thread will restart after death)//executorservice ThreadPool = Executors.newsinglethreadexecutor (); The pool adds 10 tasks for (int i=1; i<=10; i++) {Final int task = I;threadpool.execute (New Runnable () {@Overridepublic void run () { Each task is output 1 to 10for (int j=1; j<=10; J + +) {try {Thread.Sleep],} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "loop of" + j + "for Task" + Task);}});   SYSTEM.OUT.PRINTLN ("Ten task has commited"); Threadpool.shutdown (); Thread pool finishes executing after end thread//threadpool.shutdownnow (); End thread now//thread pool start timer executors.newscheduledthreadpool (3). Schedule (new Runnable () {@Overridepublic void run () { System.out.println ("bombing!");}, Timeunit.seconds,/*executors.newscheduledthreadpool (3). Scheduleatfixedrate (New Runnable () {@ overridepublic void Run () {System.out.println ("bombing!");}}, 3, timeunit.seconds); */}}
There are three ways to create a thread pool:

1, Newfixedthreadpool (n): Create n Threads

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

3. Newsinglethreadpool (): Create a single thread (the thread will be recreated after death)

The above code creates 3 threads and assigns 10 tasks, and 3 threads first perform 3 tasks in the task pool, and when a thread task finishes executing, the tasks that are not executed from the task pool continue to execute until all the tasks in the task pool are executed, the thread waits, and the last uses 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.