1

私はそのようなクラスを持っています:

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;
    }

}
4

1 に答える 1

2

お気づきのように、クラス内のメソッドVectorは同期されているため、ベクター インスタンスへのアクセスは安全です。ただし、ベクトル スレッドセーフのタスク インスタンスはありますか? そうしないと、説明したように同時アクセスからそれらを保護することができなくなります。私の提案は、このブロッキング アプローチを使用せず、共有タスク リストを使用してこの操作を実行しないようにすることです。非同期イベント駆動型アプローチを使用します。GUIスレッドとJAXB操作でこれらのタスクを更新することは、プログラムの2つの異なる操作/動作であるためです。GUI 関連のスレッドでタスクを操作し、保存 (またはその他の非 UI 関連の作業) が必要なときにエグゼキューターまたはイベントバスを使用してイベントを発生させることができます。次に、別のスレッドを実行できますJAXBもう 1 つの利点は、UI スレッドからリッスンできる保存が完了すると、コールバック イベントを取得できることです。そのため、共有リソースの保守やそれらの同期について心配する必要がなく、保守も簡単です。

于 2013-05-23T13:12:33.003 に答える