for(.......) {java
new Thread(new Runnable(){mysql
@Overridesql
public void run() {ide
//讀取文件spa
ReadUtil.read();線程
}指針
}).start();get
}同步
//-----------讀取文件的操做------------it
public synchronized static read() {
File file=new File("F://info/訂單信息.xls");
Workbook wb=null;
try {
//傳入的是文件,獲取工做薄
wb = Workbook.getWorkbook(file);
if(wb!=null){
//do .......
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
wb.close();
}
}
若是這個方法不加「同步」的話,就會報錯。空指針異常是wb.close()這塊。
我不理解的是我啓動了多個線程,應該就有多個實例ReadUtils之間的讀取文件應該互不干擾纔對,爲何一個線程會把另外一個線程的IO流給關了。。。
後臺錯誤信息以下:
Exception in thread "Thread-1" java.lang.NullPointerException
at com.excle.jxl4mysql.ReadUtil.read(ReadUtil.java:122)
at com.excle.jxl4mysql.ReadThread$1.run(ReadThread.java:36)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-2" java.lang.NullPointerException
at com.excle.jxl4mysql.ReadUtil.read(ReadUtil.java:122)
at com.excle.jxl4mysql.ReadThread$1.run(ReadThread.java:36)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-5" java.lang.NullPointerException
at com.excle.jxl4mysql.ReadUtil.read(ReadUtil.java:122)
at com.excle.jxl4mysql.ReadThread$1.run(ReadThread.java:36)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-8" java.lang.NullPointerException
at com.excle.jxl4mysql.ReadUtil.read(ReadUtil.java:122)
at com.excle.jxl4mysql.ReadThread$1.run(ReadThread.java:36)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-7" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space