루틴에 대한 코드인데 거기에 문제가 있는지 모르겠어요.

//       ,   10    ,    10           ,                         

public class IndexCreateThread {
	public static void main(String[] args) throws CorruptIndexException,
			LockObtainFailedException, IOException, InterruptedException {
		IndexWriter writer = new IndexWriter(FSDirectory.getDirectory("E:/Lucene/Index6"), new StandardAnalyzer(), true);
		LinkedList<RAMDirectory> list = new LinkedList<RAMDirectory>();
		Runnable indexListThread = new IndexListThread(list,writer);
		new Thread(indexListThread).start();
		Runnable ramIndexCreateThread = null;
		for (int i = 0; i < 15; i++) {
			ramIndexCreateThread = new RamIndexCreateThread(list);
			new Thread(ramIndexCreateThread).start();
			Thread.sleep(i*3);
		}
		Thread.sleep(5000*10);

		while (true) {
			if (list != null && list.size() == 0) {
				System.out.println(writer.docCount());
				writer.optimize();
				writer.flush();
				writer.close();
				System.out.println("over....");
				System.exit(0);
			} else {
				Thread.sleep(1000);
			}
		}
	}

}

class IndexListThread implements Runnable{
	private LinkedList<RAMDirectory> list = null;
	private IndexWriter writer = null;
	
	public IndexListThread(LinkedList<RAMDirectory> list, IndexWriter writer) {
		super();
		this.list = list;
		this.writer = writer;
	}

	@Override
	public void run() {
		while(true){
			RAMDirectory r = null;
			try {
				Thread.sleep(5);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			if(list != null && list.size() > 0){
				r = list.get(0);
				try {
					this.writer.addIndexes(new Directory[]{r});
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				list.remove(r);
				System.out.println("list     ");
			}
		}
	}
}

class RamIndexCreateThread implements Runnable {
	private LinkedList<RAMDirectory> list = null;
	public RamIndexCreateThread(LinkedList<RAMDirectory> list) {
		this.list = list;
	}

	@Override
	public void run() {
		IndexWriter ramWriter = null;
		try {
			RAMDirectory ram = new RAMDirectory();
			ramWriter = new IndexWriter(ram,new StandardAnalyzer(), true);
			List<Authors> list = new DBOperation().getAuthorses();
			for(Authors s : list){
				Field f1 = new Field("id",s.getAu_id(),Field.Store.YES,Field.Index.UN_TOKENIZED);
				Field f2 = new Field("lname",s.getAu_lname(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f3 = new Field("fname",s.getAu_fname(),Field.Store.YES,Field.Index.UN_TOKENIZED);
				Field f4 = new Field("phone",s.getPhone(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f5 = new Field("address",s.getAddress(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f6 = new Field("city",s.getCity(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f7 = new Field("zip",s.getZip(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f8 = new Field("state",s.getState(),Field.Store.YES,Field.Index.TOKENIZED);
				Field f9 = new Field("contract",s.getContract()+"",Field.Store.YES,Field.Index.TOKENIZED);
				Document doc = new Document();
				doc.add(f1);
				doc.add(f2);
				doc.add(f3);
				doc.add(f4);
				doc.add(f5);
				doc.add(f6);
				doc.add(f7);
				doc.add(f8);
				doc.add(f9);
				ramWriter.addDocument(doc);
			}
			ramWriter.flush();
			this.list.add(ram);
			System.out.println("list     ");
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				ramWriter.close();
			} catch (CorruptIndexException e) {
			} catch (IOException e) {
			}
		}
	}

}

좋은 웹페이지 즐겨찾기