1

首先,我是线程和共享变量的新手。所以请善待我;-)

我有一堂课叫Routing. 这个类接收和处理消息。如果消息属于类型A,则Routing-Object 应将其传递给ASender实现Runnable接口的对象。如果消息是类型B,则Routing-Class 应将其传递给BSenderObject。

但是ASenderBSender对象有共同的变量,应该存储到Routing-Object 中。

我现在的想法是在Routing-Object 和 getter/setter 中将变量声明为同步/易失性。

这是同步代码的正确方法吗?还是缺少什​​么?

编辑:添加了基本的代码思想。

路由类

public class Routing {

private synchronized Hashtable<Long, HashSet<String>> reverseLookup;
private ASender asender;
private BSender bsender;

public Routing() {
    //Constructor work to be done here.. 
    reverseLookup = new Hashtable<Long, HashSet<String>>();

}

public void notify(TopicEvent event) {

    if (event.getMessage() instanceof AMessage) {
        asender = new ASender(this, event.getMessage())

    } else if (event.getMessage() instanceof BMessage) {
        bsender = new BSender(this, event.getMessage())

    }
}

public synchronized void setReverseLookup(long l, Hashset<String> set) {
    reverseLookup.put(l, set);

}

public synchronized Hashtable<Long, Hashset<String>> getReverseLookup() {
    return reverseLookup;
}
}

ASender 类

public class ASender implements Runnable {

private Routing routing;
private RoutingMessage routingMessage;

public ASender(Routing r, RoutingMessage rm) {
    routing = r;
    routingMessage = rm;
    this.run();
}

public void run() {
    handleMessage();
}

private void handleMessage() {
    // do some stuff and extract data from the routing message object

    routing.setReverseLookup(somethingToSet)
}
}
4

1 回答 1

1

一些评论:

  1. Hashtable 是一个线程安全的实现,您不需要另一个“同步”关键字,请参阅以获取更多信息
  2. 避免耦合,尝试使用接口或将哈希表传递给您的发件人,请参阅了解更多信息
  3. 根据发送者的数量,您可能希望使用ConcurrentHashMap,它极大地提高了性能,请参阅Java和Java 理论与实践中的 ConcurrentHashMap 和 Hashtable :并发集合类

这将得出类似...的结论:

public interface IRoutingHandling {

    void writeMessage(Long key, HashSet<String> value);

}

public class Routing implements IRoutingHandling {

    private final Hashtable<Long, HashSet<String>> reverseLookup;

    private ASender asender;
    private BSender bsender;

    public Routing() {
        //Constructor work to be done here.. 
        reverseLookup = new Hashtable<Long, HashSet<String>>();
    }

    public void notify(TopicEvent event) {
        if (event.getMessage() instanceof AMessage) {
            asender = new ASender(this, event.getMessage())

        } else if (event.getMessage() instanceof BMessage) {
            bsender = new BSender(this, event.getMessage())

        }
    }

    @Override
    public void writeMessage(Long key, HashSet<String> value) {
        reverseLookup.put(key, value);
    }

}

public class ASender implements Runnable {

    private IRoutingHandling _routingHandling;

    public ASender(IRoutingHandling r, RoutingMessage rm) {
        _routingHandling = r;
        routingMessage = rm;
        this.run();
    }

    public void run() {
        handleMessage();
    }

    private void handleMessage() {
        // do some stuff and extract data from the routing message object

        _routingHandling.writeMessage(somethingToSetAsKey, somethingToSetAsValue)
    }

}
于 2013-07-17T10:08:29.400 回答