隨tomcat啓動的servlet來啓動線程java
public class MyExpServlet extends HttpServlet { private static final long serialVersionUID = 1L; private MyExpThread myExpThread; private MyStockThread myStockThread; public MyExpServlet() { } public void init() { String str = null; if (str == null && myExpThread == null) { myExpThread = new MyExpThread(); myStockThread = new MyStockThread(); // myExpThread.start(); // servlet 上下文初始化時啓動 socket // System.out.println("線程開始!"); //建立一個含有2個線程的線程池 ExecutorService threadPool = Executors.newFixedThreadPool(2); // 線程池中執行線程 threadPool.execute(myExpThread); threadPool.execute(myStockThread); threadPool.shutdown(); // myExpThread.interrupt(); // try { // myExpThread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } // System.out.println("線程結束!"); } } public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException { } public void destory() { if (myExpThread != null && myExpThread.isInterrupted()) { myExpThread.interrupt(); } } }
上個線程在前幾個博客,這個線程用來付款時間超過30分鐘就修改訂單狀態並將商品庫存和銷售量修改回來
數據庫
class MyStockThread extends Thread { public void run() { while (!this.isInterrupted()) {// 線程未中斷執行循環 // ------------------ 開始執行 --------------------------- //訂單失效時間及id Vector list_indiana = Constant.list_indiana; if(list_indiana!=null&&!list_indiana.isEmpty()){ for(int i=0;i<list_indiana.size();i++){ Order_Invalid order_Invalid = (Order_Invalid)list_indiana.get(i); String id = order_Invalid.getId()+""; Date enddate= order_Invalid.getEnddate(); Date sysdate = new Date(); if(sysdate.after(enddate)){ //訂單失效則修改訂單時間失效表、訂單表改狀態 RedeemManager redeemManage = (RedeemManager) ServiceLocator.getService("redeemManager"); HashMap map = new HashMap(); map.put("id", id); JmshtVO jvo1 = new JmshtVO(); jvo1.setInputMap(map); redeemManage.updateIndianaTime(jvo1); list_indiana.remove(i); }else{ break; } } Constant.list_indiana = list_indiana; } //訂單失效時間及id Vector list_ordertime = Constant.list_ordertime; if(list_ordertime!=null&&!list_ordertime.isEmpty()){ for(int i=0;i<list_ordertime.size();i++){ Order_Invalid order_Invalid = (Order_Invalid)list_ordertime.get(i); String id = order_Invalid.getId()+""; Date enddate= order_Invalid.getEnddate(); Date sysdate = new Date(); if(sysdate.after(enddate)){ //訂單失效則修改訂單時間失效表、商品加庫存、訂單表改狀態 RedeemManager redeemManage = (RedeemManager) ServiceLocator.getService("redeemManager"); HashMap map = new HashMap(); map.put("id", id); JmshtVO jvo = new JmshtVO(); jvo.setInputMap(map); redeemManage.updateOrderTime(jvo); list_ordertime.remove(i); }else{ break; } } Constant.list_ordertime = list_ordertime; } try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
線程每分鐘啓動一次,爲了不每分鐘查詢數據庫,定義一個併發容器做爲全局變量,而我須要全局變量要存全部下單的商品信息和訂單號,因此選擇list的大哥Vector,並定義實體Order_Invalid,將數據存入實體,在將實體對象存入Vector。tomcat
public static Vector<Order_Invalid> list_ordertime = new Vector<Order_Invalid>(); //Order_Invalid 普通商品 public static Vector<Order_Invalid> list_indiana = new Vector<Order_Invalid>(); //Order_Invalid 一元奪寶 public static final int time = 2;
經過線程池提供的參數進行監控。線程池裏有一些屬性在監控線程池的時候能夠使用併發
taskCount:線程池須要執行的任務數量。socket
completedTaskCount:線程池在運行過程當中已完成的任務數量。小於或等於taskCount。this
largestPoolSize:線程池曾經建立過的最大線程數量。經過這個數據能夠知道線程池是否滿過。如等於線程池的最大大小,則表示線程池曾經滿了。spa
getPoolSize:線程池的線程數量。若是線程池不銷燬的話,池裏的線程不會自動銷燬,因此這個大小隻增不+ getActiveCount:獲取活動的線程數。線程