私はそのようなクラスを持っています:
import java.util.Vector;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="task-list")
@XmlAccessorType(XmlAccessType.FIELD)
public class TaskList {
@XmlElement(name="task")
Vector<Task> tasks;
public Vector<Task> getTasks(){
return tasks;
}
}
GUIVector<Task>
内のスレッドは、含まれているものを操作するため、操作はsynchronized(vectorname){....}
衝突や矛盾を回避します。
ここで、これを XML ファイルにマーシャリングしたいと思います。TaskList
保存 (マーシャリング) 中にオブジェクトのロックを取得すると、内部のロックが発生しVector
ますか?
基本的に、ユーザーが保存ボタンを押すと、スレッドはベクトルを操作できます。エラーが発生したくないため、同期ブロックが発生したくありません。
また、2 つのスレッドが同時に同じリソースを要求した場合に発生する可能性のあるデッドロックを回避したいと考えています。どうすればそれを回避できますか? 優先度の設定は適切ですか?
タスク.java
package jaxb.classes;
import javax.xml.bind.annotation.*;
import java.util.Vector;
import java.io.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Task {
@XmlElement(name="input")
private String input; // String representing the input file
@XmlElement(name="output")
private String output; // String representing the output file
@XmlElement(name="format")
private Format format; // a jaxb.classes.Format representing the format of conversion
@XmlElement(name="taskID")
private long taskID; // a unique ID for each task.
@XmlElement(name="isReady")
public boolean isReady; // boolean value representing whether the task is ready for conversion
@XmlTransient
public boolean isChanging = false; // boolean representing if the user is changing the task DO NOT MARSHALL
@XmlTransient
public boolean isExecuting = false; // boolean representing whether the task is being executed DO NOT MARSHALL
public long getTaskID(){
return taskID;
}
public String getInput(){
return input;
}
public String getOutput(){
return output;
}
public Format getFormat(){
return format;
}
public void setOutput(String out){
output = out;
}
public void setFormat(Format f){
format = f;
}
/*
* This method will be used to create a vector
* which will be used to add row representation
* of the ask.
*/
public Vector<Object> getRowForm(){
Vector<Object> rowForm = new Vector<Object>();
rowForm.add(input);
rowForm.add(output);
rowForm.add(format.toString());
File f = new File(input);
double d = (double) (f.length()/(1024*1024));
String fileSize = String.format("%7.2f MB",d);
rowForm.add(fileSize);
return rowForm;
}
/*
* This is the only constructor that will be called
* when the user drops new file(s).
* This will be called in the AddNewTaskWindow class.
* {@param i} is a String representing input file
* {@param o} is a String representing output location
* {@param f} is a jaxb.classes.Format representing the format to convert to
* {@param taskID} is a unique ID for the task.
*/
public Task(String i, String o,Format f, long taskID){
input = i;
output = o;
format = f;
this.taskID = taskID;
}
}