zoukankan      html  css  js  c++  java
  • (CSDN迁移) JAVA多线程实现-可控最大并发数线程池(newFixedThreadPool)

    上篇文章中介绍了单线程化线程池newSingleThreadExecutor,可控最大并发数线程池(newFixedThreadPool)与其最大的区别是可以通知执行多个线程,可以简单的将newSingleThreadExecutor理解为newFixedThreadPool(1)。例如运行一下两个程序:

    单线程化线程池(newSingleThreadExecutor)示例:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class ThreadPoolByNewSingleThreadExecutor {
    
    	public static void main(String[] args) {
    		/**
    		 * 单线程化的线程池
    		 */
    		ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    		for (int i = 0; i < 10; i++) {
    			final int index = i;
    			singleThreadExecutor.execute(new Runnable() {
    				@Override
    				public void run() {
    					Thread.currentThread().setName("Thread i = " + index);
    					System.out.println(Thread.currentThread().getName() + " index = " + index);
    					try {
    						Thread.sleep(500);
    					} catch (InterruptedException e) {
    						System.out.println("exception");
    					}
    				}
    			});
    		}
    		singleThreadExecutor.shutdown();
    		System.out.println("on the main thread...");
    		
    	}
    
    }
    

    可控最大并发数线程池(newFixedThreadPool)示例:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class ThreadPoolByNewFixedThreadPool {
    	public static void main(String[] args) {
    		
    		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
    		for (int i = 0; i < 10; i++) {
    			final int index = i;
    			newFixedThreadPool.execute(new Runnable() {
    				@Override
    				public void run() {
    					Thread.currentThread().setName("Thread i = " + index);
    					System.out.println(Thread.currentThread().getName() + " index = " + index);
    					try {
    						Thread.sleep(500);
    					} catch (InterruptedException e) {
    						System.out.println("exception");
    					}
    				}
    			});
    		}
    		newFixedThreadPool.shutdown();
    		System.out.println("on the main thread...");
    	}
    
    }
    

    结果从显示上看虽然很相似,但是观察到的执行效果确实完全不一致的,newSingleThreadPool中,只有一个线程,每次输出一行后暂停0.5秒,newFixedThreadPool(3)中可以创建3个线程,一次输出3行后暂停0.5秒(当然是这三个线程都暂停0.5秒)。

    动画对比如下所示:

  • 相关阅读:
    OK335xS-Android mkmmc-android-ubifs.sh hacking
    OK335xS-Android pack-ubi-256M.sh hacking
    OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
    Qt Quick Hello World hacking
    Qt QML referenceexamples attached Demo hacking
    QT 5.4.1 for Android Ubuntu QtWebView Demo
    I.MX6 working note for high efficiency
    QT 5.4.1 for Android Windows环境搭建
    mkbootimg hacking
    Generate And Play A Tone In Android hacking
  • 原文地址:https://www.cnblogs.com/AbelZone/p/10066045.html
Copyright © 2011-2022 走看看