java多线程采集,多线程数据同步的问题。 怎么做同步啊。。

如题所述

第1个回答  2013-08-13
源代码如下:
1、线程类
package com.yjf.util; import java.util.Date; import java.util.List; public class GetWebThread extends Thread{ /** * 线程 */ public void run(){ try { while (true) { int day = 0; long time1 = new Date().getTime(); //用来同步抓取线程 synchronized("searchthead"){ Main.thisdaycount++; if(Main.thisdaycount>Main.daycount){ break; } System.out.println("开始查询第"+(Main.thisdaycount)+"天"); Thread.sleep(133); day = Main.thisdaycount-1; } //获取抓取的时间 String datetext = TimeUtil.date.format(TimeUtil.addDaysForDate(day)); String[] txt =FileUtil.getCityByTxtFile(); for(int t=0;t<txt.length;t++){ String[] way = txt[t].split("\|"); String start = way[0]; String end = way[1]; //抓取到的数据列表 List<DataBean> datalist = Main.getDataList(datetext, start, end); if(datalist!=null){ Main.isadsl = 0; CheckAdsl.adsllasttime = new Date().getTime(); FileUtil.addDataToFileCsv(datalist); Main.log.printLog("===="+datetext+"="+start+"="+end+"="+t+"=数据总数:"+datalist.size()); }else{ Thread.sleep(11); AdslThead.isadsl = true; Thread.sleep(11); //判断是否正在拨号 并暂停线程 while (AdslThead.isadsl) { Thread.sleep(5000); } t--; } } long time2 = new Date().getTime(); Main.log.printLog(datetext+"==查询完毕=========耗时"+(time2-time1)); } } catch (Exception e) { Main.log.printLog(e.getMessage()); e.printStackTrace(); } } } 第二步, 准备线程启动。
控制线程数量。 http://www.qi788.com
查询数据总天数。 设置拨号状态等。
2、线程启动类。 包括启动,控制,停止
package com.yjf.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.Timer; public class Main { private static boolean isRunGrabThread = true; //抓取线程是否执行完毕 public static int threadCount = 3; //线程数量 public static int daycount = 30; //查询总天数 public static int thisdaycount = 0; public static int isadsl = 0; public static int adslcount = 1; public static void main(String[] args) { try { startThead();//启动抓取数据主线程 } catch (Exception e) { e.printStackTrace(); } } public static void startThead(){ Thread[] grabThreads= new Thread[threadCount]; try{ //开启-数据抓取子线程 for(int i=0;i<grabThreads.length;i++){ Thread searchThread=new GetWebThread(); grabThreads[i] = searchThread; grabThreads[i].setDaemon(true); grabThreads[i].start(); } isRunGrabThread = true; //监控子线程,全部完成程序退出
WhileLoop: while(true){ //拨号策略控制 //reconnectControl(); //判断子线程是否运行完成 for(int i=0;i<threadCount;i++){ if(grabThreads[i].isAlive()){ Thread.sleep(3*1000); continue WhileLoop; } } //所有抓取线程执行完毕 isRunGrabThread = false; //子线程执行完毕则退出 break; } } catch (Exception e) { System.out.println("main主线程--系统异常!"); } } }
文章来源注明: http://qi788.com/info/11.html
相似回答