首页 > 代码库 > 线程池 40个一组去调用

线程池 40个一组去调用

package net.joystart.order.entity;

public class DrivingTrack {
	private String id;
	private String sn;
	private String idc;
	private Long gpsTime;
	private Double longitude;
	private Double latitude;
	private Integer speed;
	private Integer direction;
	private Integer altitude;
	private String showtime;
	private String showlng;
	private String showlat;
	private String flag;//选择set参数标示 add cjl 有疑问找崔金龙
	
	
	public String getShowlng() {
		return showlng;
	}
	public void setShowlng(String showlng) {
		this.showlng = showlng;
	}
	public String getShowlat() {
		return showlat;
	}
	public void setShowlat(String showlat) {
		this.showlat = showlat;
	}
	public String getShowtime() {
		return showtime;
	}
	public void setShowtime(String showtime) {
		this.showtime = showtime;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getSn() {
		return sn;
	}
	public void setSn(String sn) {
		this.sn = sn;
	}
	public String getIdc() {
		return idc;
	}
	public void setIdc(String idc) {
		this.idc = idc;
	}
	
	public Long getGpsTime() {
		return gpsTime;
	}
	public void setGpsTime(Long gpsTime) {
		this.gpsTime = gpsTime;
	}
	public Double getLongitude() {
		return longitude;
	}
	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
	public Double getLatitude() {
		return latitude;
	}
	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}
	public Integer getSpeed() {
		return speed;
	}
	public void setSpeed(Integer speed) {
		this.speed = speed;
	}
	public Integer getDirection() {
		return direction;
	}
	public void setDirection(Integer direction) {
		this.direction = direction;
	}
	public Integer getAltitude() {
		return altitude;
	}
	public void setAltitude(Integer altitude) {
		this.altitude = altitude;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	
}

 

package net.joystart.common.util.baiDu;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import net.joystart.common.util.HttpRequestUtil;
import net.joystart.common.util.baiDu.BaiDuAPIUtil.BaiduPosition;
import net.joystart.common.util.date.UtilTimeFormatter;
import net.joystart.order.entity.DrivingTrack;

public class GpsTransformation {

	@Resource
	RedisTemplate<String, String> redisTemplate;
	List<DrivingTrack> queue =  Collections.synchronizedList(new ArrayList<DrivingTrack>());

	public List<DrivingTrack> transformation(List<DrivingTrack> list) {
		int qSize = new BigDecimal(list.size()).divide(new BigDecimal(40),0,BigDecimal.ROUND_UP).intValue();
		BlockingQueue<Runnable> workQueue=new LinkedBlockingQueue<Runnable>();
		ThreadPoolExecutor pool = new ThreadPoolExecutor(1, qSize, 600, TimeUnit.SECONDS, workQueue);
		
		for(int i=0;i<qSize;i++){
			List<DrivingTrack> groupList = new ArrayList<DrivingTrack>();
			if(list.size() <= 40){
				groupList = list;
			}else{
				
				for(int j=0;j<=40;j++){
					groupList.add(list.remove(0));
				}
			}
			pool.submit(new TransformationBaidu(groupList));
		}
		while(workQueue.size()>1) {  
            try{  
            Thread.sleep(500);
            }catch(Exception ex)
            {
            	
            }
            
        }  
        
        pool.shutdown();  
          
        while(!pool.isTerminated()) {  
        	try
        	{
            pool.awaitTermination(5, TimeUnit.MINUTES);
        	}
        	catch(Exception ex)
        	{}
        }
        
        extracted();
        
		
		
		
		return queue;
	}

	@SuppressWarnings("unchecked")
	private void extracted() {
		Collections.sort(queue, new SortByID());
	}

	private class TransformationBaidu implements Runnable {

		@Resource
		RedisTemplate<String, String> redisTemplate;
		private List<DrivingTrack> list;
		

		public TransformationBaidu(List<DrivingTrack> list) {
			this.list = list;
		}

		@Override
		public void run() {
			try {
				Integer iCount = list.size();
				// gps坐标转换成百度坐标
				String url = "http://api.map.baidu.com/geoconv/v1/?";
				List<String> str = new ArrayList<String>();
				for (int i = 0; i < iCount; i++) {
					str.add(i, list.get(i).getLongitude() + "," + list.get(i).getLatitude());
				}
				String coords = String.join(";", str);
				StringBuffer param = new StringBuffer();
				param.append("ak=");
				param.append("eOfOqm3LclumR9fg7bIguqcv");
				param.append("&coords=");
				param.append(coords);
				String result = HttpRequestUtil.sendGet(url, param.toString());

				Gson gson = new Gson();

				BaiduPosition obj = gson.fromJson(result, BaiduPosition.class);
				for (int i = 0; i < iCount; i++) {
					DrivingTrack model = new DrivingTrack();
					DrivingTrack oldModel = list.get(i);
					model.setId(oldModel.getId());
					model.setLongitude(new Double(obj.getResult().get(i).getX()));
					model.setLatitude(new Double(obj.getResult().get(i).getY()));
					if(StringUtils.isEmpty(oldModel.getFlag())){
						model.setSn(oldModel.getSn());
						model.setIdc(oldModel.getIdc());
						model.setGpsTime(oldModel.getGpsTime());
						model.setSpeed(oldModel.getSpeed());
						model.setDirection(oldModel.getDirection());
						model.setAltitude(oldModel.getAltitude());
						model.setShowtime(UtilTimeFormatter.getTimeToYMDHMS(oldModel.getGpsTime()));
						model.setShowlng(getGpsLongitudeDisplayName(new Double(obj.getResult().get(i).getX())));
						model.setShowlat(getGpsLatitudeDisplayName(new Double(obj.getResult().get(i).getY())));
					}
					//String string = gson.toJson(model);

					// 将数据存入redis中
					// redisTemplate.opsForZSet().add(key, string, new
					// Double(model.getId()));
					queue.add(model);
				}
			} catch (JsonSyntaxException e) {
				e.printStackTrace();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		
		
        public String getGpsLongitudeDisplayName(Double longitude)
        {
            double t = Math.abs(longitude);
            int d = (int)Math.abs(longitude);
            int m = (int)((t - d) * 60);
            double s = ((t - d) * 60 - m) * 60;
            
            BigDecimal b = new BigDecimal(s);
            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return (longitude > 0 ? "东" : "西")+"经"+d+"°"+m+"′"+String.format("%.2f", f1)+"″";
        }

        public String getGpsLatitudeDisplayName(Double latitude)
        {
            double t = Math.abs(latitude);
            int d = (int)Math.abs(latitude);
            int m = (int)((t - d) * 60);
            double s = ((t - d) * 60 - m) * 60;
            
            
            BigDecimal b = new BigDecimal(s);
            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return  (latitude > 0 ? "北" : "南")+"纬"+d+"°"+m+"′"+String.format("%.2f", f1)+"″";
        }

	}

	class SortByID implements Comparator {
		public int compare(Object o1, Object o2) {
			DrivingTrack s1 = (DrivingTrack) o1;
			DrivingTrack s2 = (DrivingTrack) o2;
			if (new Long(s1.getId()) > new Long(s2.getId()))
				return 1;
			return 0;
		}
	}
}

 

 

 

线程池 40个一组去调用