Java多线程继承Thread类详解第1/2页
调用方法:
/** *点击量/月(年)Thread */ publicvoidyearlyClickThread(){ //获取参数 Stringyear=getPara("year"); //统计数据集X List<String>xList=newArrayList<String>(); xList.add("January"); xList.add("February"); xList.add("March"); xList.add("April"); xList.add("May"); xList.add("June"); xList.add("July"); xList.add("August"); xList.add("September"); xList.add("October"); xList.add("November"); xList.add("December"); //统计数据集Y List<Integer>yList=newArrayList<Integer>(); //统计线程状态 List<Thread>threadList=newArrayList<Thread>(); //线程状态码 intthreadStatusCode=0; //计数器 intcount=0; //每月的日志分析 for(intm=1;m<=12;m++){ //收集日期参数 List<String>dateList=newArrayList<String>(); // Stringdate=""; //判断有多少天 intdays=CalendarUtil.weekForMonth(Integer.valueOf(year),m); //组合日期 for(inti=1;i<=days;i++){ if(i<=9){ if(m<=9){ date=year+"-0"+m+"-0"+i; }else{ date=year+"-"+m+"-0"+i; } }else{ if(m<=9){ date=year+"-0"+m+"-"+i; }else{ date=year+"-"+m+"-"+i; } } dateList.add(date); } //启动线程 Threadthread=newReadLogFileThreadByYear(dateList); thread.start(); try{ //休眠 Thread.sleep(1000L); }catch(InterruptedExceptione){ e.printStackTrace(); } threadList.add(thread); } //获取线程状态 for(Threadt:threadList){ if(t.getState().toString().equals("TERMINATED")){ threadStatusCode+=1; } } //判断线程是否都执行完毕 if(threadStatusCode==12){ //接收参数 //List<Map<String,Object>>list=ReadLogFileThread.list.subList(0,12); List<Map<String,Object>>list=ReadLogFileThreadByYear.list; //设置参数 for(intp=0;p<list.size();p++){ count+=(int)list.get(p).get("clickCount"); if(list.get(p).get("month").equals("01")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("02")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("03")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("04")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("05")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("06")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("07")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("08")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("09")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("10")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("11")){ yList.add((Integer)list.get(p).get("clickCount")); }elseif(list.get(p).get("month").equals("12")){ yList.add((Integer)list.get(p).get("clickCount")); } } } setAttr("totalCount",count); setAttr("x",xList); setAttr("y",yList); renderJson(); }
线程方法:
packagecom.ninemax.util.loganalysis; importjava.io.BufferedReader; importjava.io.File; importjava.io.FileInputStream; importjava.io.IOException; importjava.io.InputStreamReader; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importcom.ninemax.util.loganalysis.tool.ConstantUtil; /** *多线程无返回值 * *@authorDarker * */ publicclassReadLogFileThreadByYearextendsThread{ //日期数组 privateList<String>clickDate; //共享数据 publicstaticList<Map<String,Object>>list=newArrayList<Map<String,Object>>(); publicReadLogFileThreadByYear(List<String>clickDate){ this.clickDate=clickDate; } /** *读取点击日志文件 * *例子:article.click.2016-05-20.txt * *@return */ publicvoidrun(){ //接收参数 Map<String,Object>map=newHashMap<String,Object>(); //利用FileInputStream读取文件信息 FileInputStreamfis=null; //利用InputStreamReader进行转码 InputStreamReaderreader=null; //利用BufferedReader进行缓冲 BufferedReaderbufReader=null; //利用StringBuffer接收文件内容容器 StringBufferbuf=newStringBuffer(); //点击量/月 intmonthClick=0; for(inti=0;i<clickDate.size();i++){ //获取文件 FileclickLogFile=newFile(ConstantUtil.LOGLOCATION,"article.click."+clickDate.get(i)+".txt"); //判断文件是否存在 if(!clickLogFile.exists()||clickLogFile.isDirectory()){ System.err.println(clickDate.get(i)+"的文件不存在..."); }else{ try{ //节点流 fis=newFileInputStream(clickLogFile); //转换流 reader=newInputStreamReader(fis,"utf-8"); //处理流 bufReader=newBufferedReader(reader); //计数器 intcount=0; //按行读取 Stringline=""; //读取文件 while((line=bufReader.readLine())!=null){ count++; //接收数据 if(!line.equals(null)&&!line.equals("")){ buf.append(line+"\n"); } } if(count==0){ count=0; }else{ count=count-1; } monthClick+=count; }catch(Exceptione){ e.printStackTrace(); }finally{ //关闭流 try{ bufReader.close(); reader.close(); fis.close(); }catch(IOExceptione){ e.printStackTrace(); } } } } map.put("month",clickDate.get(0).subSequence(5,7)); if(monthClick==0){ map.put("clickCount",0); }else{ map.put("clickCount",monthClick); } //map.put("clickContent",buf.toString()); list.add(map); } }12下一页阅读全文