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();
		}
	}

}


發佈了68 篇原創文章 · 獲贊 75 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章