java簡單實現(xiàn)多線程及線程池實例詳解
更新時間:2018年03月23日 09:35:47 作者:shao-hang
這篇文章主要為大家詳細(xì)介紹了java簡單實現(xiàn)多線程,及java爬蟲使用線程池實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了java多線程的簡單實現(xiàn)及線程池實例,供大家參考,具體內(nèi)容如下
一、多線程的兩種實現(xiàn)方式
1、繼承Thread類的多線程
/**
* 繼承Thread類的多線程簡單實現(xiàn)
*/
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、實現(xiàn)Runnable接口的多線程
/**
* 實現(xiàn)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();
}
}
}
}
二、線程池的簡單實現(xiàn)
//實現(xiàn)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)建一個具有固定線程數(shù)的線程池
ExecutorService pool = Executors.newFixedThreadPool(5);
//向線程池中提交三個線程
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
//關(guān)閉線程池
pool.shutdown();
}
}
三、java爬蟲使用線程池實例
/**
* 爬蟲調(diào)度線程池
*/
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) {
// 表示爬蟲進(jìn)入抓取狀態(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));
}
}
}
}
}
//實現(xiàn)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();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中調(diào)用https請求忽略ssl證書認(rèn)證代碼示例
在網(wǎng)絡(luò)請求中經(jīng)常會遇到需要忽略證書認(rèn)證的情況,這篇文章主要介紹了java中調(diào)用https請求忽略ssl證書認(rèn)證的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
Java獲取http和https協(xié)議返回的json數(shù)據(jù)
本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。2017-01-01
Java 中 synchronized的用法詳解(四種用法)
Java語言的關(guān)鍵字,當(dāng)它用來修飾一個方法或者一個代碼塊的時候,能夠保證在同一時刻最多只有一個線程執(zhí)行該段代碼。本文給大家介紹java中 synchronized的用法,對本文感興趣的朋友一起看看吧2015-11-11
Mybatis Plus條件構(gòu)造器ConditionConstructor用法實例解析
這篇文章主要介紹了Mybatis Plus條件構(gòu)造器ConditionConstructor用法實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

