Java-----关于线程池的使用
关于线程的相关概念不在此阐述,请百度或谷歌之
对于学习线程来说,我认为从代码开始学习比较好,前提是有一定的技术的积累,否则请关闭不用再看了~
线程池四种实现方式。
①可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
package thread; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程 public class ThreadPoolExceutorTest1 { public static void main(String[] args) { ExecutorService cachedThreadPoll = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int index = 1; try { Thread.sleep(index * 10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } cachedThreadPoll.execute(new Runnable() { public void run() { System.out.println(index); } }); } } }
②创建定长线程池,可控线程最大并发数,超出线程会在队列中等待
package thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //创建定长线程池,可控线程最大并发数,超出线程会在队列中等待 public class ThreadPoolExecutorTest2 { public static void main(String []args) { ExecutorService fixedThreadPool=Executors.newFixedThreadPool(3); for(int i=0;i<10;i++) { final int index=1; fixedThreadPool.execute(new Runnable() { public void run() { try { System.out.println(index); Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } }
③
创建定长线程池,支持定时及周期性任务执行,延迟执行示例如下(3m后执行)
package thread; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; //创建定长线程池,支持定时及周期性任务执行,延迟执行示例如下(3m后执行) public class ThreadPoolExecutorTest3 { public static void main(String []args) { ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(5); scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("this is 3 demo"); } },3,TimeUnit.MILLISECONDS); } }
定期执行 延迟1秒后,3秒执行一次
package thread; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; //定期执行 延迟1秒后,3秒执行一次 public class ThreadPoolExecutorTest35 { public static void main(String []args) { ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(5); scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println("this is 3 demo"); } },1,3,TimeUnit.MILLISECONDS); } }
④
单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
package thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。示例代码如下 public class ThreadPoolExecutorTest4 { 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() { public void run() { try { System.out.println(index); Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } }
可以使用JDK自带的监控工具来监控我们创建的线程数量,运行一个不终止的线程,创建指定量的线程,来观察:
package thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //可以使用JDK自带的监控工具来监控我们创建的线程数量,运行一个不终止的线程,创建指定量的线程,来观察: public class ThreadPoolExecutorTest45 { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { while (true) { System.out.println(index); Thread.sleep(10 * 1000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
打开jdk自带监控工具jdk-bin-jconsole.exe,以管理员身份运行
在这里可进行线程的查看~,不过程序一定要先跑起来~
代码写完~不理解的可以手敲几遍会加深理解