我想通过线程读取和写入同一个文件。通过多线程读写文件
但条件是当第一个线程完成读取时,只有另一个线程应该写入。
,我提出的解决办法是:
class XYX {
public void read(){
synchronised(fileObj){
//write the reading logic
}
}
public void write(){
synchronised(fileObj){
//write the writing logic
}
}
}
请让我知道你的想法
我想保留这个订货
读 - 写 - 读 - 写 - 读 - 等等
有了这个代码没有将突破第二个线程之前写*的第一个线程读完。 –