java多线程文件锁_Java多线程将文本混合在不工作的输出文件锁中

我有一个Java应用程序,我在其中启动100个线程。线程在解析xml文件并从中提取一些文本后,打开一个文件来写入提取的文本。但是,他们似乎混合了结果(输出文件不是按照假定的顺序)。

我使用了Lock,但它没有解决问题。谁能帮忙?

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= ("\n " + ucid +" \n" );

}

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+"

"+"\n";

}

}

if (qName.equalsIgnoreCase("abstract")) {

abstr = true;

String abst_language = attrs.getValue("lang");

if (abst_language.equals("EN")) {abstr_lang = true;

doc=doc+""+"\n" ;

}

}

if (qName.equalsIgnoreCase("description")) {

descr = true;

String des_language = attrs.getValue("lang");

if (des_language.equals("EN")) {description_lang = true;

doc=doc+""+"\n";

}

}}

@Override

public void endElement (String uri, String localName, String qName)

throws SAXException

{

if((qName.equals("abstract"))&& (abstr_lang)){

abstr_lang = false;

doc=doc+""+"\n";

}

if((qName.equals("invention-title"))&&(invention_title_lang)){

invention_title_lang = false;

doc=doc+"

"+"\n";

}

if((qName.equals("description"))&&(description_lang)){

description_lang = false;

doc=doc+""+"\n";

}

if(qName.equals("patent-document")){

doc=doc+""+"\n"+""+"\n";

//System.out.println("");

//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());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值