您好,登錄后才能下訂單哦!
本文為大家分享了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) { //創建一個具有固定線程數的線程池 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>(); //創建一個線程池 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) { // 表示爬蟲進入抓取狀態 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; } //線程執行體 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(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。