栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java简单实现多线程及线程池实例详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java简单实现多线程及线程池实例详解

本文为大家分享了java多线程的简单实现及线程池实例,供大家参考,具体内容如下

一、多线程的两种实现方式

1、继承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接口的多线程

 
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 statusMap = new HashMap(); 
  // 存放爬虫,key为爬虫的id,value为爬虫的线程池 
  static HashMap threadMap = new HashMap(); 
  //创建一个线程池 
  static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 230,80000L,  
      TimeUnit.SECONDS,  
      new ArrayBlockingQueue(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( 
    10), 
new ThreadPoolExecutor.CallerRunsPolicy()); 
   threadMap.put(spider.getId(), detailPool); 
   threadPool.execute(new threadRun(spider, threadMap)); 
 } 
      } 
    } 
  } 
} 
 
//实现Runnable接口 
class threadRun implements Runnable { 
 
  private HashMap threadPoolMap; 
  private Spiders spider; 
 
  public threadRun(Spiders spider, 
      HashMap 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(); 
    } 
  } 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141376.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号