我有一個Java應用程序,我開始100線程。線程在解析xml文件並從中提取一些文本後,打開一個文件來寫入提取的文本。但是,他們似乎混合了結果(輸出文件不是按照假定的順序)。 我用Lock
但它沒有解決問題。誰能幫忙?Java多線程混合文本不工作輸出文件鎖
Main.java
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
FileInputStream fstream = new FileInputStream("C:\\Users\\Michael\\outfilenames0.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int j=0;
while (((strLine = br.readLine()) != null) && (j<100))
{
int activethreads=Thread.activeCount();
SimpleThread t=new SimpleThread(strLine);
t.start();
if (activethreads>100)
SimpleThread.sleep(250);
if (j==99)
{j=-1;}
//System.out.println(t.getName());
j++;
}
}
}
class SimpleThread extends Thread {
private String str;
public SimpleThread(String str) {
this.str=str;
}
@Override
public void run() {
try {
Lock l=new ReentrantLock();
if (l.tryLock()){
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
// create SAX-parser...
SAXParser parser=factory.newSAXParser();
SaxHandler handler = new SaxHandler();
parser.parse(str, handler);
} catch (ParserConfigurationException ex) {
Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
}finally {l.unlock();}
} else Thread.currentThread().sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class SaxHandler extends DefaultHandler {
private boolean invention_title = false;
private boolean invention_title_lang = false;
private boolean abstr = false;
private boolean abstr_lang = false;
private boolean descr = false;
private boolean description_lang = false;
private String doc="";
private String ucid;
@Override
public void startElement(String uri, String localName,
String qName, Attributes attrs) throws SAXException {
if (qName.equals("patent-document")) {
ucid = attrs.getValue("ucid");
doc= ("<DOC>\n<DOCNO> " + ucid +"</DOCNO> \n<TEXT>");
}
if (qName.equalsIgnoreCase("invention-title")) {
invention_title = true;
String title_language = attrs.getValue("lang");
if (title_language.equals("EN"))
{
invention_title_lang = true;
doc=doc+"<TITLE>"+"\n";
}
}
if (qName.equalsIgnoreCase("abstract")) {
abstr = true;
String abst_language = attrs.getValue("lang");
if (abst_language.equals("EN")) {abstr_lang = true;
doc=doc+"<ABSTRACT>"+"\n" ;
}
}
if (qName.equalsIgnoreCase("description")) {
descr = true;
String des_language = attrs.getValue("lang");
if (des_language.equals("EN")) {description_lang = true;
doc=doc+"<DESCRIPTION>"+"\n";
}
}}
@Override
public void endElement (String uri, String localName, String qName)
throws SAXException
{
if((qName.equals("abstract"))&& (abstr_lang)){
abstr_lang = false;
doc=doc+"</ABSTRACT>"+"\n";
}
if((qName.equals("invention-title"))&&(invention_title_lang)){
invention_title_lang = false;
doc=doc+"</TITLE>"+"\n";
}
if((qName.equals("description"))&&(description_lang)){
description_lang = false;
doc=doc+"</DESCRIPTION>"+"\n";
}
if(qName.equals("patent-document")){
doc=doc+"</TEXT>"+"\n"+"</DOC>"+"\n";
//System.out.println("</DOC>");
//Lock l=new ReentrantLock();
// if (l.tryLock())
//try {
FileWrite fileWrite = new FileWrite();
try {
fileWrite.FileWrite(ucid, doc);
} catch (IOException ex) {
Logger.getLogger(SaxHandler.class.getName()).log(Level.SEVERE, null, ex);
}
// }finally {l.unlock();}
// catch (IOException ex) {
//Logger.getLogger(SaxHandler.class.getName()).log(Level.SEVERE, null, ex);
// }
}
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
if (invention_title_lang) {
doc=doc+ (new String(ch, start, length))+"\n";
}
if (abstr_lang) {
doc=doc+ (new String(ch, start, length));
}
if (description_lang) {
doc=doc+ (new String(ch, start, length));
}
}
}
class FileWrite
{
public synchronized void FileWrite(String ucid, String doc) throws IOException
{
Thread t=Thread.currentThread();
try{
FileWriter fstreamout = new FileWriter("EP-022",true);
BufferedWriter out = new BufferedWriter(fstreamout);
out.write(doc);
out.close();
if (t.isAlive())
{
t.stop();}
}
catch (Exception e)
{
System.err.println("Error"+e.getMessage());
}
}
}
你期望什麼順序? – pingw33n 2012-02-09 10:07:56