我有这样的课程:
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;
}
}
GUI 中的线程操作其中Vector<Task>
包含的内容,因此这些操作是synchronized(vectorname){....}
为了避免冲突和不一致。
现在,我想将其编组为 XML 文件。如果我在保存(编组)时获得对TaskList
对象的锁定,是否会导致对Vector
within 的锁定?
基本上,当用户按下保存按钮时,线程可以操作向量。我不希望发生错误,因此不希望出现同步块。
另外,如果两个线程同时请求相同的资源,我想避免可能发生的死锁。我怎样才能避免这种情况?设置优先级是一个不错的选择吗?
任务.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;
}
}