多线程调用VC

package sai;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Data {
	
	private static final String path = Thread.currentThread()
			.getContextClassLoader().getResource("").getPath();
	 boolean flag=false;
	 Process proc;
	public synchronized void GenerateFile(String infile, String outfile) {
     
		String inpath = path.substring(1, path.indexOf("bin"))
				+ "AdvancedARMA/Release/";
		String outpath = path.substring(1, path.indexOf("bin")) + "T/";
		String outpath2 = path.substring(1, path.indexOf("bin")) + "Txt2/";
		File file = new File(outpath2+outfile);

		if (file.exists()) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
		notify();// 唤醒阻塞队列的某线程到就绪队列
		String exeFileName = inpath + "AdvancedARMA.exe";
		String para = outpath + infile + "   " + outpath2 + outfile + "  10";

		String cmd = exeFileName + " " + para;

		try {
			proc = Runtime.getRuntime().exec(cmd);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error exec!");
		}

        finally{
        	if(proc==null){
			 proc.destroy();
			 
        	}
        	
       	
		}

		
	}

	private synchronized String getData(String filepath) {

		String inpath = path.substring(1, path.indexOf("bin")) + "Txt2/";
		String lastline = "";

		String fname = inpath + filepath;
		File file = new File(fname);
		if (!file.exists()) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
		
		notify();// 唤醒阻塞队列的某线程到就绪队列

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(fname)));
			String line;

			while ((line = br.readLine()) != null) {
				lastline = line;

			}
			br.close();
			//file.delete();//删除临时文件
		} catch (FileNotFoundException e) {
			
			System.out.println("文件正在创建中...");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lastline;
	}
    
	
	static class GenerateData extends Thread {

		private List<String> id, time, zhan;
		private boolean flag;
		private Data data;
		int xx = 0;

		public GenerateData(List<String> id, List<String> time,
				List<String> zhan, Data data) {
			this.id = id;
			this.time = time;
			this.zhan = zhan;
			this.data = data;
		}

		public void run() {
			
			for (int s = 0; s < id.size(); s++) {
				for (int z = 0; z < time.size(); z++) {
					for (int y = 0; y < zhan.size(); y++) {
						for (int i = 0; i < zhan.size(); i++) {
							
							if (y == i) {continue;}
								String in =id.get(s)+ "--" +time.get(z)
								+ "--" + zhan.get(y) + "--" + "--" + zhan.get(i)
								+ ".txt";
								String out = "temp--" + in;

								data.GenerateFile(in, out);
								xx++;
								System.out.println(xx + "\t" + out);
								System.gc();	
						}
					}
				}
			}
		}
	}

	static class GetData extends Thread {
		private List<String> id, time, zhan, data1, data2;
		private boolean flag;
		private Data data;
		private int xx = 0; // 上行
		private int yy = 0;// 下行

		public GetData(List<String> id, List<String> time, List<String> zhan,
				Data data) {
			this.id = id;
			this.time = time;
			this.zhan = zhan;
			this.data = data;
		}
        
		//上行
		public void  getsql(List<String> data1){
			
			List<String> nullList = new ArrayList<String>();
			List<String> datalist = new ArrayList<String>();
			int xx = 1;
			for (int y = 0; y < zhan.size(); y++) {
				String sql = "INSERT INTO T_ODKLTJSCB (BOARD_STATION,   ARRIVAL_STATION1,ARRIVAL_STATION2,ARRIVAL_STATION3, ARRIVAL_STATION4, ARRIVAL_STATION5,"+
	   " ARRIVAL_STATION6,ARRIVAL_STATION7,ARRIVAL_STATION8,ARRIVAL_STATION9,ARRIVAL_STATION10, ARRIVAL_STATION11,"+
	   " ARRIVAL_STATION12,ARRIVAL_STATION13,ARRIVAL_STATION14,ARRIVAL_STATION15,ARRIVAL_STATION16,ARRIVAL_STATION17,"+
		"ARRIVAL_STATION18,ARRIVAL_STATION19, ARRIVAL_STATION20,ARRIVAL_STATION21)VALUES( ";
				for (int i = 0; i < zhan.size(); i++) {
				
					if (y < i) {
						// 上行
						datalist.add("a");
					} else {
						nullList.add(null);
					}
					if (y == i) {
							for(int g=0;g<nullList.size();g++){
								sql=sql+nullList.get(g)+",";
							}
					}
				}
				for(int v=0;v<datalist.size();v++){
					sql=sql+datalist.get(v)+",";
				}
				sql= sql.substring(0,sql.length()-1);
				sql=sql+" );";
//				System.out.println("插入 sql:" +sql +"\t" + xx);
//				xx++;
				datalist.clear();
				nullList.clear();
			}
		}
		
		//下行
		public void  getsql2(List<String> data2){
			
			List<String> nullList = new ArrayList<String>();
			List<String> datalist = new ArrayList<String>();
			int xx = 1;
			for (int y = 0; y < zhan.size(); y++) {
				String sql = "INSERT INTO T_ODKLTJSCB (BOARD_STATION,   ARRIVAL_STATION1,ARRIVAL_STATION2,ARRIVAL_STATION3, ARRIVAL_STATION4, ARRIVAL_STATION5,"+
	   " ARRIVAL_STATION6,ARRIVAL_STATION7,ARRIVAL_STATION8,ARRIVAL_STATION9,ARRIVAL_STATION10, ARRIVAL_STATION11,"+
	   " ARRIVAL_STATION12,ARRIVAL_STATION13,ARRIVAL_STATION14,ARRIVAL_STATION15,ARRIVAL_STATION16,ARRIVAL_STATION17,"+
		"ARRIVAL_STATION18,ARRIVAL_STATION19, ARRIVAL_STATION20,ARRIVAL_STATION21)VALUES( ";
				for (int i = 0; i < zhan.size(); i++) {
					
					if (y > i) {
						// 下行
						datalist.add("a");
					} else{
					for(int c=0;c< zhan.size()-i;c++){
						nullList.add(null);
					}}
					if (y == i) {
						for(int v=0;v<datalist.size();v++){
							sql=sql+datalist.get(v)+",";
						}
							for(int g=0;g<nullList.size();g++){
								sql=sql+nullList.get(g)+",";
							}	
					}
				}
				sql= sql.substring(0,sql.length()-1);
				sql=sql+" );";
//				System.out.println("插入 sql:" +sql +"\t" + xx);
//				xx++;
				datalist.clear();
				nullList.clear();
			}
		}
	public void run() {
			for (int s = 0; s < id.size(); s++) {
				for (int z = 0; z < time.size(); z++) {
					for (int y = 0; y < zhan.size(); y++) {
						for (int i = 0; i < zhan.size(); i++) {

							String out = "temp--"+
								id.get(s)+ "--" +time.get(z)
								+ "--" + zhan.get(y) + "--" + "--" + zhan.get(i)
								+ ".txt";
							if (y < i) {
								// 上行
								String lastline = data.getData(out);
								//xx++;
								System.out.println("(上行)old:" + lastline
										+ "\t" + out);
								 lastline=lastline.substring(lastline.indexOf(",")+1,lastline.lastIndexOf(","));
								// 四舍五入
									lastline = String.valueOf(new BigDecimal(lastline).setScale(0,
											BigDecimal.ROUND_HALF_UP));
								System.out.println(lastline);
						    	  data1.add(lastline);
								 
							}
							if (y > i) {
								// 下行
								String lastline = data.getData(out);
								//yy++;
								System.out.println("(下行)old:" + lastline
										+ "\t" + out);
								 lastline=lastline.substring(lastline.indexOf(",")+1,lastline.lastIndexOf(","));
								// 四舍五入
									lastline = String.valueOf(new BigDecimal(lastline).setScale(0,
											BigDecimal.ROUND_HALF_UP));
									System.out.println(lastline);
						    	  data2.add(lastline);
							}
						}
					}
				}
			}
		}
	}
	
	static class GenerateTxt extends Thread {
		
		private List<String>  id,time,zhan;
		private String type,beginTime,endTime;
		public GenerateTxt(List<String> id,List<String> time,List<String> zhan,String type, String beginTime,  String endTime){
			this.beginTime=beginTime;
			this.endTime=endTime;
			this.id=id;
			this.time=time;
			this.zhan=zhan;
			this.type=type;
		}
		
		public void run() {
			String outpath = path.substring(1, path.indexOf("bin")) + "T/";	
			for(int a=0;a<id.size();a++){
			for(int t=0;t<time.size();t++){
				for (int y = 0; y < zhan.size(); y++) {
				for(int i=0;i<zhan.size();i++){
					  if(y==i){
						  //上行y<i //下行y>i									
						  continue;
					  }
					  String sql="select rownum id,  tjsj,bz from( select  substrb(tjrq,0,8),tjrq,tjsj,c.bz from t_odkltj a ,holiday_date1 c where a.KLTJ_ID="+id.get(a)+" and  c.dayy=substrb(tjrq,0,8) and a.sjlx="+type+" and a.board_station='"+zhan.get(y)+"' and " +
					  		"a.arrival_station='"+zhan.get(i)+"' AND a.tjrq like'%"+time.get(t)+"'and c.dayy between'"+beginTime+"'and '"+endTime+"' order by a.tjrq) ";
					
					  String out = outpath+id.get(a)+ "--" +time.get(t)
						+ "--" + zhan.get(y) + "--" + "--" + zhan.get(i)
						+ ".txt";
					  System.out.println(sql);
					// DBTest.query(sql,out);
					
				}
				}
				}
			}
			
		}
	}


	
public static void main(String args[]) {
		
		
		List<String> time=new ArrayList<String>();
		time.add("0500-0530");
		time.add("0530-0600");
		time.add("0600-0630");
		time.add("0630-0700");
		time.add("0700-0730");
		time.add("0730-0800");
		time.add("0800-0830");
		time.add("0830-0900");
		time.add("0900-0930");
		time.add("0930-1000");
		time.add("1000-1030");
		time.add("1030-1100");
		time.add("1100-1130");
		time.add("1130-1200");
		time.add("1200-1230");
		time.add("1230-1300");
		time.add("1300-1330");
		time.add("1330-1400");
		time.add("1400-1430");
		time.add("1430-1500");
		time.add("1500-1530");
		time.add("1530-1600");
		time.add("1600-1630");
		time.add("1630-1700");
		time.add("1700-1730");
		time.add("1730-1800");
		time.add("1800-1830");
		time.add("1830-1900");
		time.add("1900-1930");
		time.add("1930-2000");
		time.add("2000-2030");
		time.add("2030-2100");
		time.add("2100-2130");
		time.add("2130-2200");
		
		List<String> zhan=new ArrayList<String>();
		zhan.add("AOH");
		zhan.add("SHH");
		zhan.add("SXH");
		zhan.add("NEH");
		zhan.add("ASH");
		zhan.add("VQH");
		zhan.add("KNH");
		zhan.add("AIH");
		zhan.add("KAH");
		zhan.add("SZH");
		zhan.add("ITH");
		zhan.add("IFH");
		zhan.add("WXH");
		zhan.add("VCH");
		zhan.add("QYH");
		zhan.add("CZH");
		zhan.add("DYH");
		zhan.add("RUH");
		zhan.add("ZJH");
		zhan.add("BWH");
		zhan.add("XPH");
		zhan.add("NJH");
		
		List<String> kltjlx= new ArrayList<String>();  
		kltjlx.add("OD_半小时");
	
		
		List<String> TJZDZ= new ArrayList<String>();
		TJZDZ.add("南京-上海,");
		DBTest db= new DBTest();
		List<String> id = db.getid(kltjlx, TJZDZ);// kltjlx 客流统计类型   TJZDZ 统计字段值 
		String type="2";  // 半小时
		String beginTime="20110331"; //开始时间
		String endTime="20110406"; //结束时间
		
	//	long start = System.currentTimeMillis();
				
		Data data= new Data();
		
		Thread t1=new Thread(new GenerateData(id,time,zhan,data));
		Thread t2=new Thread(new GetData(id,time,zhan,data));
		Thread t3=new Thread(new GenerateTxt(id,time,zhan,type,beginTime,endTime));
		t3.run();
		if(!t3.isAlive()){
		t1.setPriority(Thread.MAX_PRIORITY);	
		t1.start();
		
		try {
			t2.sleep(1000*60);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  t2.start();
		  try {
			t1.join();
			 t2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}}
////		long end = System.currentTimeMillis();
//		System.out.println("程序启动时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(start))+"\n程序结束时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(end))+"\n程序读取时间: " + new SimpleDateFormat("mm:ss").format(new Date(end - start)));
		
	
}
}

你可能感兴趣的:(java)