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
package com.dev.btse.data;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.battmonitor.base.Com;
import com.battmonitor.sql.MysqlConnPool;
 
public class TmpSensor_TestDataThread extends Thread{
    private MysqlConnPool pool;
    
    public static int reacord_interval = 10*60;            //Êý¾Ý´æ´¢¼ä¸ô
 
    private static Map<Integer, TmpSensor_SaveDataThread> myThreads = new HashMap<>();
    
    public TmpSensor_TestDataThread(MysqlConnPool conn_pool) {
        this.pool = conn_pool;
    }
    
    @Override
    public void run() {
        System.out.println("TmpSensor_TestDataThread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
        TmpSensor_SQL.createHisDataBase(pool);
        
        try {
            while(true) {
                List<TmpSensor_inf> tmpinfs = TmpSensor_SQL.queryAllTmpSensor(this.pool);                //²éѯËùÓд«¸ÐÆ÷É豸
                
                
                if(tmpinfs.size() > 0) {
                    for(int i =0;i<tmpinfs.size();i++) {
                        TmpSensor_inf inf = tmpinfs.get(i);
                        
                        TmpSensor_SaveDataThread thread = myThreads.get(inf.getSensor_dev_id());
                        if(thread == null || (!thread.isRunning)) {
                            
                            System.out.println(inf.getSensor_dev_id() + "ÀúÊ·Êý¾Ý¼Ç¼Ïß³ÌÆô¶¯ ¡£¡£¡£¡£¡£");
                            thread = new TmpSensor_SaveDataThread(inf, pool);
                            myThreads.put(inf.getSensor_dev_id(), thread);
                            thread.start();
                            
                        }
                    }
                }
                try {
                    Thread.sleep(60*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * ´ÓmapÖÐÒÆ³ýÖ¸¶¨µÄ¶ÔÏó
     * @param sensor_dev_id
     */
    public static void removeThreadFromMap(int sensor_dev_id) {
        synchronized(myThreads) {
            myThreads.remove(sensor_dev_id);
        }
    }
}