我正在尝试在 Java 线程和与共享对象的同步方面获得一些经验并执行简单的锁定。我要做的是创建 Java 应用程序,它使用 2 个不同的线程将时间戳(或分钟)插入文本文件:一个线程将只插入奇数时间戳(或分钟)到文件,另一个线程将只插入偶数时间戳到相同文件。当一个线程正在插入时,另一个线程不能插入并等待直到收到通知。文件内容必须在进程关闭之后(控制台中的 Ctrl+C),如下所示:
2013-05-10 21:37:02 2013-05-10 21:37:03 2013-05-10 21:37:04 2013-05-10 21:37:05
或者
2013-05-10 21:37 2013-05-10 21:38 2013-05-10 21:39 2013-05-10 21:40
首先,我只想创建一个线程,它将在文件中插入行,只是无法弄清楚出了什么问题。这是我的代码:
import java.io.*;
public class MyFileWriter
{
private FileWriter fwriter;
private BufferedWriter bufwriter;
public FileWriter getWriter()
{
return this.fwriter;
}
public void setWriter(FileWriter pfwriter)
{
this.fwriter = pfwriter;
}
public BufferedWriter getBufWriter()
{
return this.bufwriter;
}
public void setBufWriter(BufferedWriter pbfwriter)
{
this.bufwriter = pbfwriter;
}
public static void main(String[] args)
{
MyFileWriter myfile = new MyFileWriter();
try
{
FileWriter fstream = new FileWriter("output.txt");
myfile.setWriter(fstream);
BufferedWriter out = new BufferedWriter(fstream);
myfile.setBufWriter(out);
}
catch (IOException e)
{
System.out.println("Error : "+e.getMessage());
}
MyThread mt = new MyThread();
mt.setBufWriter(myfile.getBufWriter());
mt.start();
}
}
class MyThread extends Thread
{
private BufferedWriter bout;
private int count = 1;
public BufferedWriter getBufWriter()
{
return this.bout;
}
public void setBufWriter(BufferedWriter pbout)
{
this.bout = pbout;
}
public void run()
{
try
{
this.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Error : "+e.getMessage());
}
try
{
this.bout.write("String # "+count);
this.bout.newLine();
}
catch(IOException e)
{
System.out.println("Error : "+e.getMessage());
}
this.count++;
}
}
我希望在创建并启动 MyThread mt 之后,线程将插入到文件“output.txt”字符串中,并且在我在命令提示符中执行 Ctrl+C 之后,我会得到这样的结果:
字符串 #1 字符串 #2 字符串 #3
但是应用程序本身由于某种原因完成,我只得到空文件(有时文件在我启动应用程序时有“String #1”,但大部分是空的)。谁能启发我我做错了什么?
操作系统:Windows XP SP3,Java 版本:
java 版本 "1.7.0_21" Java(TM) SE Runtime Environment (build 1.7.0_21-b11) Java HotSpot(TM) Client VM (build 23.21-b01, 混合模式, 共享)