如何正确同步以下情况?

时间:2019-06-21 19:40:23

标签: java model-view-controller synchronization threadpool

我正在为uni创建一个应用程序,该应用程序模拟用于发送和接收电子邮件的客户端服务器软件。我想了解以下实现是否可以正确同步服务器端对文件的读写,它似乎可以正常工作,但是我并不能百分百保证它的意义。对于每个用户,我都有一个已接收电子邮件的文件和一个已发送电子邮件的文件。

情况如下,我有N个任务,每个任务由一个专用的类管理,更确切地说,我具有以下内容:LoginTask,ReceiveTask,SendTask和DeleteTask,它们之间的代码非常相似,因此我不再赘述全部。

发送任务

public class SendTask extends Thread implements Runnable {

private final ServerModel model;
private final Socket incoming;
private final ObjectOutputStream out;
private final ObjectInputStream in;

public SendTask(Socket s, ServerModel model, ObjectOutputStream outObj, ObjectInputStream inObj) {
    this.out = outObj;
    this.in = inObj;
    this.incoming = s;
    this.model = model;
    setDaemon(true);
}

.... some code ....

            boolean validUsers = true;
            for (String receiver : receiverToString) {
                if (!model.checkUser(receiver)) {
                    out.writeObject("false");
                    validUsers = false;
                    break;
                } 
            }

            if(validUsers) {
                model.writeSended(emailToString);
                model.writeReceived(emailToString);               
                out.writeObject("confermo");                                
            }

删除任务

public class DeleteTask extends Thread implements Runnable {

private final ServerModel model;
private final Socket incoming;
private final ObjectOutputStream out;
private final ObjectInputStream in;
private final String user;

public DeleteTask(Socket s, ServerModel model, ObjectOutputStream outObj, ObjectInputStream inObj, String user) {
    this.out = outObj;
    this.in = inObj;
    this.incoming = s;
    this.model = model;
    this.user = user;
    setDaemon(true);
}

@Override
public void run() {
    try {
        String email = (String)in.readObject();
        String fileType = (String)in.readObject();
        String outcome = model.deleteEmail(email, user, fileType);

        out.writeObject(outcome);
        incoming.close();
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(DeleteTask.class.getName()).log(Level.SEVERE, null, ex);
    }

}

在服务器端,我有一个名为model的对象,该对象在文件上进行读写操作以保存和加载电子邮件,每次执行任务时,我都会在ServerViewController类中将对该对象的引用传递给该对象:

                case "send":
                    Runnable sendTask = new SendTask(incoming, serverModel, outObj, inObj);
                    exec.execute(sendTask);                            
                    break;

                case "delete": 
                    String userD = (String)inObj.readObject();                        
                    Runnable deleteTask = new DeleteTask(incoming, serverModel, outObj, inObj, userD);
                    exec.execute(deleteTask);

最后,在我的ServerModel类中(由从控制器传递给任务的模型对象引用),对文件进行读写的所有签名都声明为同步:

public synchronized String deleteEmail(String mail, String user, String type) throws IOException {
public synchronized void writeReceived(String[] emailToString) throws IOException, ClassNotFoundException, InterruptedException {
public synchronized void writeSended(String[] emailToString) throws IOException {
public synchronized ArrayList<String> loadUserReceivedEmails(String username) throws IOException {
public synchronized ArrayList<String> loadUserSendedEmails(String username) throws IOException {

我想知道模型对象是否由于定义而在它们之间同步,因为它们是同一对象,或者不是这样,我的同步是否正常工作?声明同步方法以正确管理文件是否足够?

在签名中忽略同步并在如下所示的任务类中写入一些块会更好或更合理吗

synchronized(model) { ... task do your things on the model ... }

???

0 个答案:

没有答案