ファイル内のデータを共有する複数のGatlingシミュレーションを実行したいと思います。Java/Scalaマルチスレッドファイル書き込み
今私は、次のコードを持っています。ファイルは、それが含まれているかどう
import java.io.BufferedWriter
import java.io.File
import java.io.FileNotFoundException
import java.io.FileWriter
import java.io.IOException
import java.util.Scanner
import scala.collection.mutable.ListBuffer
class AppIDLocker(fileName:String) {
var available = true
def acquire() = synchronized {
while (!available) wait()
available = false
}
def release() = synchronized {
available = true
notify()
}
def TestAndUse(id:String):Boolean = {
acquire()
var list = new ListBuffer[String]()
try {
val file = new File(fileName)
try {
val scanner = new Scanner(file)
while(scanner.hasNextLine()) {
list += scanner.nextLine()
}
scanner.close()
} catch {
case e: IOException => println("Had an IOException trying to read the file for AppIdLocker")
}
try {
val fw = new FileWriter(file, true)
val bw = new BufferedWriter(fw)
if (list.contains(id)) {
release()
return false //the ID has been used by an other Officer already
}
else{
bw.write(id + "\n")
bw.flush()
bw.close()
release()
return true //the ID is appended, and ready to be used by the Officer, who called the method
}
} catch {
case e: IOException => println("Had an IOException trying to write the file for AppIdLocker")
return false
}
} catch {
case e: FileNotFoundException => println("Couldn't find file for AppIDLocker.")
return false
}
}
}
TestAndUseは、文字列とチェックを受けます。 yesの場合はfalseを返し、そうでない場合は文字列をファイルに書き込みます。 シミュレーションでは数百人の仮想ユーザーが完全に動作しますが、並列実行のシミュレーションでは動作しません。私は、2つの並列実行されているシミュレーションでは、230行がファイルに書き込まれ、2つは同じだったことに気付きました。
他の実行中のシミュレーションでファイルを開いている間、ファイルをロックする方法を教えてください。
は、もしあなたが複数のシミュレーションプロセスを実行していることを意味し、「平行シミュレーションを実行する」ことで、 ヴィクトル