Giter Site home page Giter Site logo

engine's Issues

关于write的并发问题

write函数中的这部分代码似乎会产生并发问题

        // ...
        int hash = valueFileHash(numkey);
        int off = valueOffsets[hash].getAndIncrement();
        try {
            ByteBuffer keyBuffer = keyMappedByteBuffers[hash].slice();
            keyBuffer.position(keyOffsets[hash].getAndAdd(KEY_AND_OFF_LEN));
            keyBuffer.putLong(numkey).putInt(off);
       // ...

假设两个线程同时调用write方法并且产生的hash相同时,有如下执行顺序:

  1. t1执行keyBuffer.position() -> buffer位置变成了x
  2. t2执行keyBuffer.position() -> buffer位置变成了x + 16
  3. t1执行keyBuffer.putLong() -> buffer中为 [key1]
  4. t2执行keyBuffer.putLong() -> buffer中为 [key1, key2]
  5. t1执行keyBuffer.putInt() -> buffer中为 [key1, key2, off1]
  6. t2执行keyBuffer.putInt() -> buffer中为 [key1, key2, off1, off2]

此时似乎应该会产生不一致的问题。
没有任何恶意,希望能学习一下😊

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.