java簡單實現多線程及線程池實例詳解
更新時間:2018年03月23日 09:35:47 作者:shao-hang
這篇文章主要為大家詳細介紹了java簡單實現多線程,及java爬蟲使用線程池實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了java多線程的簡單實現及線程池實例,供大家參考,具體內容如下
一、多線程的兩種實現方式
1、繼承Thread類的多線程
/** * 繼承Thread類的多線程簡單實現 */ public class extThread extends Thread { public void run(){ for(int i=0;i<100;i++){ System.out.println(getName()+"-"+i); } } public static void main(String arg[]){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); if(i==50){ new extThread().start(); new extThread().start(); } } } }
2、實現Runnable接口的多線程
/** * 實現runable接口的多線程實例 */ public class runThread implements Runnable { public void run(){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); } } public static void main(String arg[]){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"-"+i); if(i==50){ runThread rt = new runThread(); new Thread(rt,"新線程1").start(); new Thread(rt,"新線程2").start(); } } } }
二、線程池的簡單實現
//實現Runnable接口 class TestThread implements Runnable{ public void run() { for(int i = 0;i < 100;i++){ System.out.println(Thread.currentThread().getName() + "i的值為:" + i); } } } public class threadPoolTest { public static void main(String[] args) { //創(chuàng)建一個具有固定線程數的線程池 ExecutorService pool = Executors.newFixedThreadPool(5); //向線程池中提交三個線程 pool.submit(new TestThread()); pool.submit(new TestThread()); pool.submit(new TestThread()); //關閉線程池 pool.shutdown(); } }
三、java爬蟲使用線程池實例
/** * 爬蟲調度線程池 */ public class threadPool { public static HashMap<String, Spiders> statusMap = new HashMap<String, Spiders>(); // 存放爬蟲,key為爬蟲的id,value為爬蟲的線程池 static HashMap<Integer, ThreadPoolExecutor> threadMap = new HashMap<Integer, ThreadPoolExecutor>(); //創(chuàng)建一個線程池 static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 230,80000L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy()); public static void executeThread(Spiders spider) { statusMap.put(String.valueOf(spider.getId()), spider); // 爬蟲有效 if (spider.getFlag() == 0) { if (spider.getStatus() == 0) { // 表示爬蟲進入抓取狀態(tài) ThreadPoolExecutor detailPool = null; if (threadMap.get(spider.getId()) == null) { detailPool = new ThreadPoolExecutor(30, 80, 80000L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>( 10), new ThreadPoolExecutor.CallerRunsPolicy()); threadMap.put(spider.getId(), detailPool); threadPool.execute(new threadRun(spider, threadMap)); } } } } } //實現Runnable接口 class threadRun implements Runnable { private HashMap<Integer, ThreadPoolExecutor> threadPoolMap; private Spiders spider; public threadRun(Spiders spider, HashMap<Integer, ThreadPoolExecutor> threadPoolMap) { this.threadPoolMap = threadPoolMap; this.spider = spider; } //線程執(zhí)行體 public void run() { try { if ("rong360".equals(spider.getWebsite())) { new RongThread(threadPoolMap.get(spider.getId()), spider) .startSpider(); } else if ("xxgg_sd".equals(spider.getWebsite())) { new Spider_ShanDong(threadPoolMap.get(spider .getId()), spider).startSpider(); } else if ("xxgg_gz".equals(spider.getWebsite())) { new Spider_GuiZhou(threadPoolMap.get(spider .getId()), spider).startSpider(); } else if ("sx".equals(spider.getWebsite())) { new SpiderSX(spider).startSpider(); } else if ("baidu".equals(spider.getWebsite())) { new SpiderBaiDu(spider).startSpider(); } else if ("11315".equals(spider.getWebsite())) { new Spider11315ByName(spider).startSpider(); } } catch (Exception e) { e.printStackTrace(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Web MVC框架學習之配置Spring Web MVC
這一篇文章講的是Spring Web MVC各部分的配置方法,包括Java代碼配置和XML文件配置以及MVC命名空間的使用方法。2017-03-03Kotlin中l(wèi)et、run、with、apply及also的用法和差別
作用域函數是Kotlin比較重要的一個特性,分為5種let、run、with、apply及also,這五個函數的工作方式非常相似,但是我們需要了解這5種函數的差異,以便在不同的場景更好的利用它,這篇文章主要介紹了Kotlin中l(wèi)et、run、with、apply及also的差別,需要的朋友可以參考下2023-11-11SpringSecurity在SpringBoot中的自動裝配過程
這篇文章主要介紹了SpringSecurity在SpringBoot中的自動裝配過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07解決?IDEA?Maven?項目中"Could?not?find?artifact"?
這篇文章主要介紹了解決IDEA Maven項目中Could not?find?artifact問題的常見情況和解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07詳解SpringMVC @RequestBody接收Json對象字符串
這篇文章主要介紹了詳解SpringMVC @RequestBody接收Json對象字符串,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01