我寫了一個Runnable類Java的可運行文件IO
private static class Processing implements Runnable {
private static File file;
private static byte[] message;
public Processing (File f, byte[] m){
this.file = f;
this.message = m;
}
public void run(){
WriteToFile(file , message);
... other processing....
}
private static void WriteToFile(File f , byte[] msg){
FileOutputStream fs = null;
try {
fs = new FileOutputStream(f) ;
fs.write( msg);
fs.flush();
fs.close();
}catch (Exception e){
e.printStackTrace();
}
}
在主類
public class MainClass{
public void somemethod(){
....
(new Thread(new Processing (<somefile> , <somebytes>))).start();
....
}
}
我的問題是
- 是這個方法正確嗎?我的願望是提高文件io(在 WriteToFile方法),因爲MainClass被超過1000個 用戶調用。
如何在代碼中模擬1000個用戶?是
for (int i = 0 ; i<1000 ...){ (new Thread(new Processing (<somefile> , <somebytes>))).start(); }
感謝