English 中文(简体)
方法的同步想法
原标题:synchronization ideas for a method

我有一个多读的A类,可使用另一个B类(A只有一个B类)的<代码>insert()方法。

如果不使整个方法同步进行,是否有更好的办法使以下方法同步进行? (减少同步管理)

private void insert(byte[] shardKey, byte[] queueKey, 
            byte[] value, PipelineMessageType msgType) {
        PipelineMessage pipelineMessage = new PipelineMessage(queueKey, 
                value, msgType);
        LinkedBlockingQueue<PipelineMessage> queue;
        JedisShardInfo shardInfo = shardedJedis.getShardInfo(shardKey);     // shardedJedis is an instance variable of this class
        String mapKey = shardInfo.getHost() + shardInfo.getPort();          
        queue = shardQueue.get(mapKey);         // shardQueue is an instance variable of this class                         
        boolean insertSuccessful = queue.offer(pipelineMessage);
        if(!insertSuccessful) {
            // perform the pipeline sync - flush the queue
            // use another thread for this

            // (processing of queue entries is given to another thread here)

            // queue would be empty now. Insert (k,v)
            queue.offer(pipelineMessage);
        }
    }

我只试图使接触实例变数的碎块同步,但可能会出现一种情况,即两条镜子试图插入一个完整的电线,并进入<条码>。 然后,两条路透镜可能会处理我不想要的点名。

欢迎任何建议。 事先感谢你。

问题回答

请允许我指出,如果JedisShardInfo只是一个只读的项目,那么你就应该保护/理顺这一议程项目。 因此,你只能从线上同步。

queue= ...

否则,除了第一次发言(宣布输油管信息)外,几乎所有东西都应同步进行,然后我真心想,与宣布整个方法同步相比,这一变化是否很大。

Also, if you got other points of synchronization, I mean other methods or block codes that are synchronized on this, you should consider splitting them and synchronize on different data members of this depending on which data members you wish to protect from multi-threading :

 Object lockerA = new Object() {};

 synchronized( lockerA )
 {}//sync

说:

Regards, Stéphane

The key to correct synchronization is to follow this pattern:

synchronize(lockObjectForState) { // All code that alters state must also synchronise on the same lock

    while(!stateOkToProceed()) {
        try {
            lockForState.wait();   
        } catch (InterruptedException e) {
            // handle if your thread was interrupted deliberately as a single to exit, or spuriously (in which case do nothing)
        }
    }

    updateState();

    lockForState.notifyAll();
}

<代码>java.util.con 当前 包裹提供了解决共同翻新问题的所需班级的许多校对安全实施。 考虑使用Blocking Queue





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签