相关疑难解决方法(0)

什么是可重入锁定和概念?

我总是感到困惑.有人会解释Reentrant在不同背景下的含义吗?为什么要使用折返与非折返?

说pthread(posix)锁定原语,它们是否可以重入?使用它们时应该避免哪些陷阱?

互斥是重入的吗?

multithreading locking pthreads

81
推荐指数
4
解决办法
4万
查看次数

同步与重入锁定的性能

在针对多线程系统的Queue实现时,我遇到了一些惊喜.这是:-

场景: - 1个生产者,1个消费者: - 生产者将整数放入队列中.消费者只是将其从队列中删除.

队列的基础数据结构: - TreeSet(我从未想过会使用),LinkedList,LinkedBlockingQueue(具有不确定的大小)

代码: - TreeSet作为队列: -

while (i < 2000000) {
        synchronized (objQueue) {

            if (!(objQueue.size() > 0)) {
                try {
                    objQueue.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Integer x = objQueue.first();
            if (x != null) {
                objQueue.remove(x);
                ++i;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:-

      while (i < 2000000) {
        synchronized (objQueue) {
            objQueue.add(i);
            ++i;
            objQueue.notify();
        }
    }
Run Code Online (Sandbox Code Playgroud)

对于LinkedBlockingQueue: -

     while (i < 2000000){
        try {
            objQueue.put(i);
            ++i; …
Run Code Online (Sandbox Code Playgroud)

java multithreading

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

multithreading ×2

java ×1

locking ×1

pthreads ×1