java-thread pool and Atom (automic)

Source: Internet
Author: User

1.Executors class

|-java.util.concurrent.Executors It can provide a variety of functions of the thread pool
static method:

Executorservice newfixedthreadpool (int nthreads); Create a fixed-size thread pool

Executorservice Newcachedthreadpool ();//create a cache thread pool

Executorservice newsinglethreadexecutor ();//create a single thread pool

Scheduledexecutorservice newscheduledthreadpool (int);//create thread pool for timed processing

2.ExecutorService

Member Methods:

     void shutdown();//The thread that performs all the tasks is in the wait state, so the last need
The shutdown method of the executors class destroys the thread that finishes the task

void Execute(Runnable command);//can add multiple tasks

Future<t> Submit (callable<t> Task);//Call method is called in the task to return the result to the future

3.ScheduledExecutorService

     schedule(Runnable command, long delay, TimeUnit unit);//Create and execute a one-time operation enabled after a given delay

Thread Pool Start Timer
Calling the Scheduledexecutorservice schedule method, the returned Schedulefuture object can cancel the task.
Support interval Repetitive task timing mode, does not directly support the absolute timing mode, need to convert to a relative time mode.

4.completionservice<v>

All known implementation classes: Executorcompletionservice It is an enhanced version of the Submit method in Executorservice, which can submit multiple callable<t> tasks, Consumer take processes their results in the order in which they were completed (the Get method returns the results).

Member Methods:

Future<V> take();//Gets and removes the future that represents the next completed task, and waits if no such task exists at this time.

Future<V> submit(Callable<V> task);//Submits the value to execute to return the task and returns the future that represents the result of the pending task.

//the <T> future<t> submit (callable<t> Task) method under the Executorservice class usesExecutorservice pool=Executors.newsinglethreadexecutor (); Future<String> Future=pool.submit (NewCallable<string>() {@Override PublicString Call ()throwsException {//TODO auto-generated Method StubSystem.out.println (Thread.CurrentThread (). GetName ()); Thread.Sleep (3000); return"Hahaha";      }        }); System.out.println ("Wait for results"); System.out.println ("Result:" +future.get ()); //Executorcompletionservice Class DemoCompletionservice<integer> service=NewExecutorcompletionservice<integer> (Executors.newfixedthreadpool (3)); //Submit a 10 task       for(inti=0;i<10;i++)      {          Final intT=i+1; Service.submit (NewCallable<integer>() {@Override PublicInteger Call ()throwsException {thread.sleep (NewRandom (). Nextint (5) *1000); returnT;      }        }); }      //Take out the results in chronological order       for(intj=0;j<10;j++) {System.out.println (Service.take (). get ()); }

//Thread Pool Demo: The Executors Class (static method) provides a variety of functions for the thread pool      /*Note: You can add any task to go in.                      (Void execute (Runnable command) method under the Executorservice Class) * Creates a fixed-size thread pool: executors.newfixedthreadpool (3); 3 performs a task to automatically start 3 threads Create a cache thread pool: executors.newcachedthreadpool ();//The system will intelligently allocate how many threads you add to create a single thread pool based on how many tasks you have added: execut Ors.newsinglethreadexecutor ();//The system will ensure that a thread is always doing the task for you, and if that thread dies, the system creates another thread instead.               Create a thread pool for timed processing: Executors.newscheduledthreadpool (3). Schedule () Method Note: For threads that have completed all tasks, the wait state is required, so the last             The shutdown method of the executors class destroys the thread that finishes the task. * */Executorservice Pool=executors.newfixedthreadpool (3);        Executors.newcachedthreadpool ();        Executors.newsinglethreadexecutor (); //Add a task         for(inti=0;i<5;i++)        {            Final intTask =i+1; Pool.execute (NewRunnable () {@Override Public voidrun () { for(intj=0;j<10;j++) System.out.println (Thread.CurrentThread (). GetName ()+ "Perform +task+" tasks: "+ (j+1));         }            }); } System.out.println ("Shutdown"); Pool.shutdown ();

5.java.util.concurrent.atomic under

There are many atomic (synchronous) operations on basic data, even member variables in a class

java-thread pool and Atom (automic)

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.