今天忽然須要用到線程池來解決一個問題,可是已經好久沒有寫關於線程的程序了,記憶中曾經記憶深入的線程池demo彷佛也已經模糊起來。事實證實了一句話,學知識就得溫故而知新,隨着時間的流逝,記憶中的知識也會慢慢消退,只有經過不斷的回顧,不斷的思考才能將知識用的爲所欲爲,隨時須要,隨時即可信手拈來。java
雖然今天遇到的問題在百度的幫助下順利獲得解決,可是,回來仍是不甘心的找了很久之前寫的源碼,真心以爲仍是值得細細品味。最開始接觸線程時,也和不少朋友同樣,裏面有太多的問號,雖然聽得比較多,可是就實際應用卻難以開始在代碼上實現。今天就把源碼貼出來,供本身時時溫習,與此同時也給新朋友提供一個線程的入門案例,以便更能理解這項至關有特點的技術。web
首先仍是介紹一下線程和線程池吧,我並無詳細地去研究過線程和線程池,因此在此談的就是本身的見解和理解,不會像書中所說的那麼高深,說實話,對於一個剛剛接觸線程的人來講,看書是真心看不懂,講的專業術語太多,很難理解。可是也不排除一些特別擅長看書的人,他們就對書上的內容很容易接受,這也是相比較於那些實際動手的人而言。tomcat
在如今的不少應用中都須要線程來完成任務,若是線程越多,那麼完成任務所用的時間就越少,前提是線程數量也得合適才行,若是單時間內產生大量的線程可能會是系統的內存忽然達到其極限值,這是很很差的。線程池無非就是在最開始得時候就放不少線程在那裏,須要的時候直接去拿就行。這好像又和鏈接池有點相似。線程的種類確實比較多,我也不列舉了,願意瞭解的能夠百度,貼出實現源碼demo,體驗一番線程和多線程的魅力:多線程
1. 新建web工程:在這裏經過web.xml中Servlet的<load-on-startup>來啓動線程,當demo一旦在tomcat中被啓動,線程池就會馬上運行,執行在開始給他分配的任務。app
項目包結構以下:jsp
2. Web.xml中的源碼:ide
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>InitSevlet</servlet-name> <servlet-class>init.InitSevlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>InitSevlet</servlet-name> <url-pattern>/servlet/InitSevlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3. Servlet中源碼:學習
package init; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import util.Threadpool; import work.MainWork; import work.SingleWork; public class InitSevlet extends HttpServlet { private static final long serialVersionUID = -5708566856710493294L; public void destroy() { super.destroy(); } public void init() throws ServletException { MainWork mainWork = new MainWork("Red"); MainWork mainWork1 = new MainWork("Blue"); MainWork mainWork2 = new MainWork("Gray"); MainWork mainWork3 = new MainWork("張三"); MainWork mainWork4 = new MainWork("李四"); SingleWork SingleWork = new SingleWork("Red"); SingleWork SingleWork1 = new SingleWork("Blue"); SingleWork SingleWork2 = new SingleWork("Gray"); SingleWork SingleWork3 = new SingleWork("張三"); SingleWork SingleWork4 = new SingleWork("李四"); List<MainWork> list = new ArrayList<MainWork>(); list.add(mainWork); list.add(mainWork); list.add(mainWork); list.add(mainWork); list.add(mainWork); list.add(mainWork); list.add(SingleWork); list.add(SingleWork1); list.add(mainWork1); list.add(SingleWork2); list.add(mainWork2); list.add(SingleWork3); list.add(mainWork3); list.add(mainWork4); list.add(SingleWork4); ExecutorService threadpool = Threadpool.getInstance(); try { List<Future<Object>> lists = threadpool.invokeAll(list); } catch (InterruptedException e) { e.printStackTrace(); } } }
4. 線程池的定義類:網站
package util; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Threadpool { private static volatile ExecutorService threadpool; private final static int THREADCOUNT = 5; //線程池數量 public Threadpool() { } public static synchronized ExecutorService getInstance() { if (null == threadpool) { threadpool = Executors.newFixedThreadPool(THREADCOUNT); } return threadpool; } }
5. 兩個任務中須要注意的就是他們的繼承不同,SingleWork extends MainWork ;MainWork implements java.io.Serializable, Callable<Object>this
MainWork.java
package work; import java.util.concurrent.Callable; public class MainWork implements java.io.Serializable, Callable<Object> { private static final long serialVersionUID = 9092827859545735164L; public MainWork() { } public MainWork(String workname) { this.workname = workname; } private String workname; public String getWorkname() { return workname; } public void setWorkname(String workname) { this.workname = workname; } public Object call() throws Exception { System.out.println("hello, " + getWorkname()); Thread.sleep(5000); // 睡眠5秒 return null; } }
SingleWork .java
package work; public class SingleWork extends MainWork { private static final long serialVersionUID = -6170744685310914685L; private String name; public SingleWork() { } public SingleWork(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public Object call() throws Exception { System.out.println("hi, " + getName()); return null; } }
5. 程序運行結果:
貼源碼於此,反覆理解,反覆學習。
能經過堅持去解決的問題,那就拼着命咬着牙堅持下去。
郵箱:it_red@sina.com
我的博客: http://itred.cnblogs.com 網站: http://wangxingyu.jd-app.com
本文版權歸做者和博客園共有,歡迎轉載,但請在文章顯眼位置標明文章出處。未經本人書面贊成,將其做爲他用,本人保留追究責任的全部權利。