如何使用java同步文件操作?同步还是锁定?
我使用JSCH库创建了一个用于读写远程位置属性文件的应用程序 我想同步写入操作 这是我的密码如何使用java同步文件操作?同步还是锁定?,java,multithreading,concurrency,readwritelock,reentrantreadwritelock,Java,Multithreading,Concurrency,Readwritelock,Reentrantreadwritelock,我使用JSCH库创建了一个用于读写远程位置属性文件的应用程序 我想同步写入操作 这是我的密码 class WebApp{ private String webappName; private boolean isQA = false; private String path ; public WebApp(String name , String path , boolean isQA){ this.webappName = name;
class WebApp{
private String webappName;
private boolean isQA = false;
private String path ;
public WebApp(String name , String path , boolean isQA){
this.webappName = name;
this.path = path;
this.isQA = isQA;
}
public String getWebappName() {
return webappName;
}
public void setWebappName(String webappName) {
this.webappName = webappName;
}
public boolean isQA() {
return isQA;
}
public void setQA(boolean isQA) {
this.isQA = isQA;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
class WebAppProperty implements Runnable{
private WebApp webapp; // webapp-1
private String propertyFile; // server.properties
private String keyValue;
public String getPropertyFile() {
return propertyFile;
}
public void setPropertyFile(String propertyFile) {
this.propertyFile = propertyFile;
}
@Override
public void run(){
writeToPropertyFile();
}
public WebAppProperty(WebApp webapp , String propertyFile,String keyValue){
this.webapp = webapp;
this.propertyFile = propertyFile;
this.keyValue = keyValue;
}
private void writeToPropertyFile(){
try{
// code for writing key value pair into remote file
}catch (Exception e) {
}
}
}
这就是我想要的
- 如果用户尝试写入web应用程序上的属性文件(如“a”),则尝试写入同一web应用程序的同一文件的所有其他用户应等待,但其他用户可以写入同一web应用程序的另一个文件或另一web应用程序上同名的文件 同步方法可以完成所有这些工作吗?它如何识别webapp和属性文件
注意:我看到了关于并发性的不同问题,但这些问题并没有消除我的疑虑,这就是我提出新问题的原因,请帮助我对此进行澄清。请按如下方式编写Webapp类:
class final WebApp{
private final String webappName;
private final boolean isQA = false;
private final String path ;
public WebApp(String name , String path , boolean isQA){
this.webappName = name;
this.path = path;
this.isQA = isQA;
}
public String getWebappName() {
return webappName;
}
public boolean isQA() {
return isQA;
}
public String getPath() {
return path;
}
class final WebAppProperty implements Runnable{
private final WebApp webapp; // webapp-1
private finalString propertyFile; // server.properties
private final String keyValue;
public String getPropertyFile() {
return propertyFile;
}
@Override
public void run(){
writeToPropertyFile();
}
public WebAppProperty(WebApp webapp , String propertyFile,String keyValue){
this.webapp = webapp;
this.propertyFile = propertyFile;
this.keyValue = keyValue;
}
private void writeToPropertyFile(){
try{
// code for writing key value pair into remote file
}catch (Exception e) {
}
}
}
编写webappProperty类,如下所示:
class final WebApp{
private final String webappName;
private final boolean isQA = false;
private final String path ;
public WebApp(String name , String path , boolean isQA){
this.webappName = name;
this.path = path;
this.isQA = isQA;
}
public String getWebappName() {
return webappName;
}
public boolean isQA() {
return isQA;
}
public String getPath() {
return path;
}
class final WebAppProperty implements Runnable{
private final WebApp webapp; // webapp-1
private finalString propertyFile; // server.properties
private final String keyValue;
public String getPropertyFile() {
return propertyFile;
}
@Override
public void run(){
writeToPropertyFile();
}
public WebAppProperty(WebApp webapp , String propertyFile,String keyValue){
this.webapp = webapp;
this.propertyFile = propertyFile;
this.keyValue = keyValue;
}
private void writeToPropertyFile(){
try{
// code for writing key value pair into remote file
}catch (Exception e) {
}
}
}
如果使用这种方法,则不需要使用synchronized。这两个类都是不可变的类。不可变引用可以在多线程环境中自由共享,而无需任何同步。这是因为一旦创建了不可变引用,它将在对象的生命周期内冻结。只有通过创建新对象,才能对对象进行所需的任何更改
请注意,如果不按上述方式创建不可变类,则必须在webapp类中的所有方法中使用synchronized,因为无论使用哪种方法,都需要保护共享的可变状态。第二节课也一样。否。
synchronized
没有帮助。你选择的方法限制了你的选择。我看到的唯一方法是“锁定”文件。您应该检查特殊文件是否存在,如果它没有创建它,请修改您的属性文件并删除“锁定”文件。@talex我已经检查了锁定实现,令人困惑的是如何仅锁定同一文件上的写入操作?意味着如果一个线程试图写入文件a,则没有其他线程可以写入文件a,但它应该能够写入文件a以外的其他文件。如何使用锁实现这一点?我的理解是,我们可以锁定我们需要的操作performing@AnsarSamad你们能分享你们创建WebAppProperty实例并运行它的部分吗?你们能告诉我,它如何允许一个线程允许写入文件A.properties,同时阻止其他线程写入同一个文件,但允许它们写入其他文件?