Java Executor framework is launched after Jdk1.5, for a more convenient framework for developing multi-threaded applications and packaged;
Compared to traditional Thread class, Java Executor easy to use, better performance, easier to manage, and support the thread pool, so we developed when the reptiles, in order to improve the efficiency of crawling reptiles, we want to use multiple threads, it is recommended to use Java the Executor framework to achieve, because Executor framework is both simple and efficient;
Java Executor framework used in reptile applications
Common Interface:
Create a fixed number of threads in the thread pool.
public static ExecutorService newFixedThreadPool(int nThreads)
Execute a thread
void java.util.concurrent.Executor.execute(Runnable command)
View the number of active threads
int java.util.concurrent.ThreadPoolExecutor.getActiveCount()
End all threads
void java.util.concurrent.ExecutorService.shutdown()
Description: Executor in the management of multiple threads when processing will be carried out effective arrangements, such as when created, the thread pool is 10, if the actual thread over 10, Executor will effectively blocking queue and scheduling. For us this is transparent to the developer, has absolutely no specific implementation within it;
Test Case
1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 import java.util.concurrent.ThreadPoolExecutor; 4 import java.util.concurrent.atomic.AtomicInteger; 5 6 /** 7 * @author zsh 8 * @site www.qqzsh.top 9 * @company wlgzs 10 * @create 2019-06-02 10:57 11 * @description 12 */ 13 public class ExecutorTest { 14 15 // 执行标识 16 Private static Boolean exeFlag = to true ; . 17 18 is public static void main (String [] args) throws InterruptedException { . 19 // Create ExecutorService fixed connection pool 10 created initial thread 20 is ExecutorService ExecutorService = Executors.newFixedThreadPool (2 ); 21 is of AtomicInteger AtomicInteger = new new of AtomicInteger (); 22 is 23 is the while (exeFlag) { 24 IF (atomicInteger.get () <= 100 ) { 25 executorService.execute ( new new Runnable() { 26 @Override 27 public void run() { 28 System.out.println("爬取了第"+atomicInteger.get()+"网页..."); 29 atomicInteger.getAndIncrement(); 30 } 31 }); 32 }else { 33 if (((ThreadPoolExecutor)executorService).getActiveCount() == 0){ 34 executorService.shutdown(); 35 exeFlag=false; 36 System.out.println ( "reptiles task has been completed" ); 37 [ } 38 is } 39 40 the Thread.sleep (( Long ) 0.1 ); 41 is } 42 is } 43 is }