多线程访问文件的问题 用synchronized关键字将文件操作用synchronized(this){ 文件操作。}包含起来就OK了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的建议是你多个线程用一个file对象,在用文件的时候对file对象进行同步,修改代码如下:class SimpleThread extends Thread{ private int countDown = 5; private static int threadCount = 0; private static File f ; private int threadNumber = ++threadCount; public SimpleThread() { System.out.println("Making "+threadNumber); if(f!=null) { f = new File("test.property"); } if (!f.exists()) { System.err.println("Error: property file not found!"); return ; } } public void run() { while(true) { System.out.println("Thread "+ threadNumber + "("+countDown + ")"); if (--countDown ==0) { return; } else { try { getNewDevNo("firewall"); } catch (Exception ex) { System.out.println(ex); } } } } public static void main(String[] args) throws Exception { for (int i=0; i<5; i++) { new SimpleThread().start(); } System.out.println ("All Threads Started"); } private synchronized void getNewDevNo(String devType) throws Exception { Properties prop = new Properties(); synchronized(f) { FileInputStream fileis = new FileInputStream(f); prop.load(fileis); fileis.close(); } int newDevNo = 0; String oldDevNo = prop.getProperty(devType); if (oldDevNo == null) { throw new NoSuchElementException(); } newDevNo = Integer.parseInt(oldDevNo) + 1; synchronized(f) { prop.setProperty(devType,String.valueOf(newDevNo)); FileOutputStream fileos = new FileOutputStream(f); prop.store(fileos,"DevNo"); fileos.close(); } System.out.println ("Found new device, number is: "+newDevNo); } } linux环境下java开发问题 关于JAVA实现计算器的核心代码问题。 SWT界面用了asyncExec后假死???!! Collection问题 java捕捉信息 Java 出现死循环,不知道哪里错了? java中一个通过除法获得了一个3位小数乘以100后变成了16为小数 jboss java好让人伤心啊!竟然捕捉不好鼠标事件! 菜鸟一枚,想自学JAVA,求指导入门... 请问在panel板上贴图用什么方法? 请问哪位有jcreator的注册码,高分相送
class SimpleThread extends Thread
{
private int countDown = 5;
private static int threadCount = 0;
private static File f ;
private int threadNumber = ++threadCount;
public SimpleThread() { System.out.println("Making "+threadNumber);
if(f!=null)
{
f = new File("test.property");
}
if (!f.exists()) {
System.err.println("Error: property file not found!");
return ;
}
} public void run() {
while(true) {
System.out.println("Thread "+ threadNumber + "("+countDown + ")");
if (--countDown ==0) {
return;
}
else {
try {
getNewDevNo("firewall"); }
catch (Exception ex) {
System.out.println(ex);
}
}
}
} public static void main(String[] args) throws Exception
{
for (int i=0; i<5; i++) {
new SimpleThread().start();
}
System.out.println ("All Threads Started"); } private synchronized void getNewDevNo(String devType) throws Exception
{ Properties prop = new Properties();
synchronized(f)
{
FileInputStream fileis = new FileInputStream(f);
prop.load(fileis);
fileis.close();
} int newDevNo = 0;
String oldDevNo = prop.getProperty(devType);
if (oldDevNo == null) {
throw new NoSuchElementException();
} newDevNo = Integer.parseInt(oldDevNo) + 1;
synchronized(f)
{
prop.setProperty(devType,String.valueOf(newDevNo)); FileOutputStream fileos = new FileOutputStream(f);
prop.store(fileos,"DevNo");
fileos.close();
}
System.out.println ("Found new device, number is: "+newDevNo);
}
}