本文为大家分享了java多线程的简单实现及线程池实例,供大家参考,具体内容如下
一、多线程的两种实现方式
1、继承Thread类的多线程
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 继承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接口的多线程
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/**
* 实现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();
}
}
}
}
|
二、线程池的简单实现
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//实现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爬虫使用线程池实例
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/**
* 爬虫调度线程池
*/
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();
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/William_hangshao/article/details/67644335
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 63
-
2025-05-25 41
-
2025-05-25 97
-
2025-05-27 22
-
2025-06-04 40
热门评论