Multi-threaded code and condition variable usage

A multi-threaded piece of code accesses a resource (eg: a filesystem) asynchronously. To achieve this, I'll use condition variables. Suppose the `FileSystem` is an interface like: class FileSystem { // sends a read request to the fileSystem read(String fileName) { // ... // upon completion, execute a callback callback(returnCode, buffer); } } I have now an application accessing the `FileSystem`. Suppose I can issue multiple reads through a `readFile()` method. The operation should write data to the byte buffer passed to it. // constructor public Test() { FileSystem disk = ... boolean readReady = ... Lock lock = ... Condition responseReady = lock.newCondition(); } // the read file method in quesiton public void readFile(String file) { try { lock.lock(); // lets imagine this operation needs a lock // this operation may take a while to complete; // but the method should return immediately; while (!readReady) { //

以上就是Multi-threaded code and condition variable usage的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准