共享servlet的fileIO? [英] Shared servlet for fileIO?

查看:100
本文介绍了共享servlet的fileIO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个 webservice servlet ,它可以执行一些 fileIO
如何确保如果多个客户端执行相应的WS方法,那么fileIO仍然只能由一个线程处理?因此,没有数据丢失,因为几个客户端触发fileIO?$ / b>
$ b

我应该搜索哪个方向?

解决方案

最好的办法是让读取/写入的同步对象的底层方法。 (例如File对象可能是最简单的)。

  private static File theFile = new File(theonetoopen.txt); 
...

private void someImportantIOMethod(Object stuff){
synchronized(theFile){

//您的文件输出在此处写入代码。

}
}


Imagine having a webservice or servlet, which can perform some fileIO. How can I make sure that if more than one client executes the according WS method, that the fileIO is still handled only by one single "thread"? So that no data is lost because several clients trigger the fileIO?

Which direction should I search for?

解决方案

The best bet is likely to just make the underlying method(s) that read/write synchronize on a common object. (eg. the File object could be easiest).

private static File theFile = new File("theonetoopen.txt");
...

private void someImportantIOMethod(Object stuff){
    synchronized(theFile){

    //Your file output writing code here.

    }
}

这篇关于共享servlet的fileIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆