The concept of threading in a traditional JDK is briefly described in the previous section, followed by a description of what changes have been made to threading after Jdk5.
First, the following java.util.concurrent package has a word packet atomic ( atomic ) package, some of which provide atomic operation classes, respectively:
1 Atomicboolean, 2 atomicinteger,atomicintegerarray,atomicintegerfieldupdater<t>,3 Atomiclong, Atomiclongarray,atomiclongfieldupdater<t>,4 atomicreference<v> Atomicreferencearray<e>,atomicreferencefieldupdater<t,v>,5 Atomicmarkablereference<v>,6 atomicstampedreference<v>
The atomic Package atomically operates on basic data in the basic data, the basic data in the array, and in the class.
After introducing the basic implementation of the atomic Operation class in a multithreaded environment, we then introduce the application of the thread concurrency library.
Thread pool concept, the thread pool is a form of multithreading that adds tasks to the queue during processing, and then automatically starts those tasks after the thread is created.
example, the Tomcat server design idea.
Thread pool implementation: In a thread run implementation, while (querying if there is a task = = in a pool of threads) {Execute a task}
The executes class is provided by default in the JDK, which provides static methods for creating different types of thread pools.
For everyone in the online pool learning, just pay attention to two points, the number of threads in the thread pool, and how many tasks we have lost to the thread pool.
We do not need to leave the task to the thread to run, but simply throw the task into the thread pool, which is automatically run by the thread pool, and we can handle the threads with a variety of mechanisms when there are no idle threads in the thread pools. This is handled as follows:
1. Create a fixed number of connection pools.
1Executorservice pool = Executors.newfixedthreadpool (3);2 for(inti = 0; I < 10; i++) {3 Final intTask =i;4Pool.execute (NewRunnable () {5 @Override6 Public voidrun () {7 for(inti = 0; I < 10; i++) {8 Try {9Thread.Sleep (20);Ten}Catch(interruptedexception e) { One } ASystem.out.println (Thread.CurrentThread (). GetName () + ": Is looping of" +i+ "to task is" +task); - } - } the }); -}
Execution result of the thread:
1Pool-1-thread-3: is looping of 0 forTask is 22Pool-1-thread-1: is looping of 0 forTask is 03Pool-1-thread-2: is looping of 0 forTask is 14Pool-1-thread-2: is looping of 1 forTask is 15Pool-1-thread-1: is looping of 1 forTask is 06Pool-1-thread-3: is looping of 1 forTask is 27Pool-1-thread-2: Is looping of 2 forTask is 18Pool-1-thread-1: Is looping of 2 forTask is 09Pool-1-thread-3: Is looping of 2 forTask is 2TenPool-1-thread-2: Is looping of 3 forTask is 1 OnePool-1-thread-3: Is looping of 3 forTask is 2 APool-1-thread-1: Is looping of 3 forTask is 0
As we can see from the results, we created a thread pool object with a fixed length of three threads, and 10 task tasks were lost to the thread pools, and during the execution the tasks were executed simultaneously by 3 threads, and in the process of 3 threads execution, the 4th task did not execute and was suspended in the queue. Because we are creating a fixed number of thread pools at this point, the 4th task will not be executed until one of the 3 threads ends.
This is a fixed number of thread pool-fixed thread pool, after all tasks are completed, the main task does not die, because the thread pool thread does not die, if you want the main program to die, you need to execute the shutdown () or Shutdownnow () method.
2. Create a cached thread pool
1Executorservice pool =Executors.newcachedthreadpool ();2 for(inti = 0; I < 10; i++) {3 Final intTask =i;4Pool.execute (NewRunnable () {5 @Override6 Public voidrun () {7 for(inti = 0; I < 10; i++) {8 Try {9Thread.Sleep (20);Ten}Catch(Interruptedexception e) {} OneSystem.out.println (Thread.CurrentThread (). GetName () + ": Is looping of" +i+ "to task is" +task); A } - } - }); the}
The results of the implementation are as follows:
Pool-1-thread-1: is looping of 0 forTask is 0Pool-1-thread-3: is looping of 0 forTask is 2Pool-1-thread-5: is looping of 0 forTask is 4Pool-1-thread-7: is looping of 0 forTask is 6Pool-1-thread-2: is looping of 0 forTask is 1Pool-1-thread-4: is looping of 0 forTask is 3Pool-1-thread-6: is looping of 0 forTask is 5Pool-1-thread-8: is looping of 0 forTask is 7Pool-1-thread-10:is Looping of 0 forTask is 9Pool-1-thread-9: is looping of 0 forTask is 8Pool-1-thread-1: is looping of 1 forTask is 0
The program automatically creates the appropriate number of threads to handle the corresponding number of tasks, which is the thread pool with the cache.
It automatically adds new threads when the thread service does not come in time. The number of threads in the thread pool is not fixed,
When the number of thread service tasks changes, after a period of time we will take the thread back, not wasted.
3. Create a thread pool of individual threads, similar to single-threaded.
1Executorservice pool =executors.newsinglethreadexecutor ();2 for(inti = 0; I < 10; i++) {3 Final intTask =i;4Pool.execute (NewRunnable () {5 @Override6 Public voidrun () {7 for(inti = 0; I < 10; i++) {8 Try {9Thread.Sleep (500);Ten}Catch(Interruptedexception e) {} OneSystem.out.println (Thread.CurrentThread (). GetName () + ": Is looping of" + i + "for task is" +task); A } - } - }); the } -
Execution Result:
Pool-1-thread-1: is looping of 0 forTask is 0Pool-1-thread-1: is looping of 1 forTask is 0Pool-1-thread-1: Is looping of 2 forTask is 0Pool-1-thread-1: Is looping of 3 forTask is 0Pool-1-thread-1: Is looping of 4 forTask is 0Pool-1-thread-1: Is looping of 5 forTask is 0Pool-1-thread-1: is looping of 6 forTask is 0Pool-1-thread-1: is looping of 7 forTask is 0Pool-1-thread-1: is looping of 8 forTask is 0Pool-1-thread-1: is looping of 9 forTask is 0Pool-1-thread-1: is looping of 0 forTask is 1Pool-1-thread-1: is looping of 1 forTask is 1Pool-1-thread-1: Is looping of 2 forTask is 1Pool-1-thread-1: Is looping of 3 forTask is 1Pool-1-thread-1: Is looping of 4 forTask is 1
As you can see from the results, when creating a single thread, the task is performed separately in the order in which it was placed, that is, when the first task finishes executing the next task in the creation of a thread (or the original name), a thread is guaranteed to exist, and the thread is restarted after it is resolved.
4. Timer thread pool, start timer with thread pool.
Scheduledexecutorservice Executorservice=executors.newscheduledthreadpool (3); Executorservice.scheduleatfixedrate (new Runnable () { publicvoid Run () { System.out.println ("execute1"); 2, 1 , timeunit.seconds); Executorservice.schedule (new Runnable () { publicvoid run () { System.out.println ("Execute2"); 2, Timeunit.seconds);
For the thread pool operation of the timer,
Schedule, how often do you start the execution;
scheduleatfixedrate, the number of times to start the execution once, and then the number of seconds to execute again, and is not affected by the task execution time;
Schedulewithfixeddelay, the number of times to start the execution once, and then the number of seconds to execute again, and the task execution time is affected;
JAVA5 new operations for threading-concurrent Package Introduction