java简单实现多线程及线程池实例详解
程序员文章站
2022-06-05 08:36:20
本文为大家分享了java多线程的简单实现及线程池实例,供大家参考,具体内容如下
一、多线程的两种实现方式
1、继承thread类的多线程
/**
* 继...
本文为大家分享了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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 爆笑生活雷事多。