import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import javax.sound.sampled.LineEvent; import org.apache.mina.filter.codec.textline.LineDelimiter; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sunrise.psmis.Constants; /** * * <pre> * Copyright (C): 2012 * * Description: * 测试 * * Revision History: * 2012-10-23 initial version. * * </pre> */ public class SeqTestFile { private static final int aInit = 1; private static final int bInit = 1000; private static final String fileName="C:\\seqtest.txt"; /** * @param args */ public static void main(String[] args) { SeqTestFile seqTestFile = new SeqTestFile(); seqTestFile.testSequence(); } /** * 不使用线程 */ public void testSequenceNoThreads(){ String seqenceName = SequenceEnum.EXP.toString(); for(int i=0;i<bInit;i++){ if(i%2==0){ seqenceName =SequenceEnum.EXP.toString(); } else{ seqenceName =SequenceEnum.WGROUP.toString(); } long val = SequenceUtil.getInstance().getNextVal(seqenceName); System.out.println(val+""); } } /** * 使用线程 */ public void testSequence(){ InitFile(); String SeqenceName ; Thread thread; for(int i=0;i<aInit;i++){ if(i%2==0){ SeqenceName =SequenceEnum.EXP.toString(); } else{ SeqenceName =SequenceEnum.WGROUP.toString(); } thread = new Thread(new SequenceThread(SeqenceName,i)); try { thread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // SequenceUtil.getInstance().resetSequence(SeqenceName); thread.start(); } } public static void InitFile(){ try { File file=new File(fileName); if(!file.exists()){ file.createNewFile(); } BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write("000000000000000"); writer.newLine(); writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class SequenceThread implements Runnable{ private String sequenceName; private int threadNo; public SequenceThread(String sequenceName,int threadNo){ this.sequenceName = sequenceName; this.threadNo = threadNo; } @Override public void run() { BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); writer = new BufferedWriter(new FileWriter(fileName, true)); for(int i=0;i<bInit;i++){ long val = SequenceUtil.getInstance().getNextVal(sequenceName); String line =""; Boolean ifFound=false; while((line=reader.readLine())!=null){ if(line.equals(sequenceName+val)){ System.out.println("找到相同的序列:"+sequenceName+val); ifFound = true; break; } } writer.write(sequenceName+val); writer.newLine(); } writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(reader!=null) reader.close(); if(writer!=null) writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
最近下载更多