首页 \ 问答 \ 领导者在DelayQueue中使用的究竟是什么?(What exactly is the leader used for in DelayQueue?)

领导者在DelayQueue中使用的究竟是什么?(What exactly is the leader used for in DelayQueue?)

我想了解java.util.concurrent DelayQueue ,但leader让我困惑。

首先,我们可以实现一个没有这样的leader的DelayQueue:

public boolean offer(E e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        q.offer(e);

        if (q.peek() == e) {
            available.signal();
        }
        return true;
    } finally {
        lock.unlock();
    }
}

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        for (;;) {
            E first = q.peek();
            if (first == null) {
                available.await();
            } else {
                long delay = first.getDelay(TimeUnit.NANOSECONDS);
                if (delay <= 0) {
                    return q.poll();
                } else {
                    available.awaitNanos(delay);
                }
            }
        }
    } finally {
        lock.unlock();
    }
}

其次,似乎并没有最小化不必要的定时等待 。 根据注释:

Leader-Follower模式的这种变体( http://www.cs.wustl.edu/~schmidt/POSA/POSA2/ )用于最小化不必要的定时等待

我认为最大限度地减少awaitNanos (使用await而不是awaitNanos ),但我真的怀疑这一点。 如果新元素不是队列的头部,则不会发送线程信号。 (见下面的offer方法)

if (q.peek() == e) {
    leader = null;  // set leader to null
    available.signal();
}

所以只有当新元素是头时才会有所作为。 但是在这种情况下, leader将被设置为null ,并且被通知的线程不会以这种方式进行( take方法):

else if (leader != null)
    available.await();

线程将始终执行awaitNanos

那么,有人可以向我解释吗? 我在某个地方得到了错误的想法吗?


I was trying to understand DelayQueue in java.util.concurrent, but leader confused me.

First of all, we can implement a DelayQueue without leader like this:

public boolean offer(E e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        q.offer(e);

        if (q.peek() == e) {
            available.signal();
        }
        return true;
    } finally {
        lock.unlock();
    }
}

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        for (;;) {
            E first = q.peek();
            if (first == null) {
                available.await();
            } else {
                long delay = first.getDelay(TimeUnit.NANOSECONDS);
                if (delay <= 0) {
                    return q.poll();
                } else {
                    available.awaitNanos(delay);
                }
            }
        }
    } finally {
        lock.unlock();
    }
}

Secondly, it seems does not minimize unnecessary timed waiting. According to the annotation:

This variant of the Leader-Follower pattern (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to minimize unnecessary timed waiting

I take that as minimizing awaitNanos (using await instead of awaitNanos) but I really doubt that. If the new element isn't the head of the queue, then no thread will be signalled. (see offer method as below)

if (q.peek() == e) {
    leader = null;  // set leader to null
    available.signal();
}

So it only makes a difference when new elements is the head. But in this case, the leader will be set to null, and the signalled thread won't go this way(take method):

else if (leader != null)
    available.await();

The thread will always do awaitNanos.

So, can anybody explain that to me? Did I got the wrong idea somewhere?


原文:https://stackoverflow.com/questions/48493830
更新时间:2024-03-01 21:03

最满意答案

您确定仅提供客户端ID是否足够? 根据他们的文档 ,你应该提供access_token

https://api.instagram.com/v1/users/search?q=jack&access_token=ACCESS-TOKEN

响应:

{
    "data": [{
        "username": "jack",
        "first_name": "Jack",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_66_75sq.jpg",
        "id": "66",
        "last_name": "Dorsey"
    },
    {
        "username": "sammyjack",
        "first_name": "Sammy",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
        "id": "29648",
        "last_name": "Jack"
    },
    {
        "username": "jacktiddy",
        "first_name": "Jack",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
        "id": "13096",
        "last_name": "Tiddy"
    }]
}

正如您在响应中看到的,第一个结果是确切的用户名“jack”。


I fix this issue by count = 10 and then filter it with the exact username

相关问答

更多
  • JSON数据已移至其他网址: https : //www.instagram.com/ {USER_NAME} /?__ a = 1 JSON data has been moved to another URL: https://www.instagram.com/{USER_NAME}/?__a=1
  • 实际上你正在链接到$post->images->standard_resolution->url 现在,如果我们看看API返回的内容( https://www.instagram.com/developer/endpoints/users/ ) }, "link": "http://instagr.am/p/BWrVZ/", "user": { "username": "kevin", "profile_picture": "h ...
  • 您确定仅提供客户端ID是否足够? 根据他们的文档 ,你应该提供access_token : https://api.instagram.com/v1/users/search?q=jack&access_token=ACCESS-TOKEN 响应: { "data": [{ "username": "jack", "first_name": "Jack", "profile_picture": "http://distillery.s3.amazona ...
  • 哦,这样可以解决问题。 在循环结果后需要循环遍历数组。 //instagram api example, this code makes you fetch images after searching specific hashtag $(document).ready(function() { var myId = "------"; //sets so form doesnt override jquery $('#submit').click(funct ...
  • 据我所知,您只会获得沙盒用户的关注者/关注信息。 因此,您可以邀请沙盒用户,开始关注他,然后应该显示它。 From what I know, you will only get the followers/following info for your sandbox users. So you could invite a sandbox user, start following him and then it should be displayed.
  • 好吧,现在这解决了这个问题: json_encode($ tagMedia); 和 foreach( (array) $ tagMedia-> data as $ entry) alright, now this solved the problem: json_encode($tagMedia); and foreach ((array)$tagMedia->data as $entry)
  • 您不能再使用Instagram API获取其他用户的关注者,您只能获得经过身份验证的用户的关注者/关注列表。 您需要通过Instagram审核并批准您的应用并进入实时模式以访问API中的所有数据,并且您还需要“followers_list”范围权限。 问题的第一部分中的代码使用私有Instagram API,但不支持获取关注者列表。 (简而言之,你只是混合了来自各地的代码,它甚至没有关联,一个是公共api和其他私有api,你需要阅读文档) You cannot get other user's follow ...
  • 经过长时间的试用n错误得到了这个 因为我有media_id ,我想检查它不被用户喜欢 并且当前登录的用户access_token ,因此使用以下用户我能够找到用户是否喜欢该媒体 请检查以下参数的说明 max_like_id = $ media_id //媒体ID count = 1 //我只想获取并仅检查一个媒体 access_token = {$ insta_access_token} //它为当前用户提供access_token $is_liked_url = "https://api. ...
  • success处理函数的data参数由JSON ajax.php返回的任何内容填充,结构将相应地匹配。 看起来该对象的images属性只有图像的URL数组而没有其他数据。 您需要更新PHP脚本的这一部分,以返回不仅仅是图像的URL数组,还包括从Instagram API检索的其他数据。 尝试将最后一部分更新为: echo json_encode(array( 'next_id' => $media->pagination->next_max_id, 'images' => $media-> ...
  • doc说您可以传递count参数: 在存在分页的视图中,我们还支持“count”参数。 只需将其设置为您想要接收的项目数。 请注意,对于大多数应用程序,默认值应该没问题 - 但如果您决定增加此数字,则每个端点上都会定义一个最大值。 我没有在你的测试页面中看到它,所以我试了一下。 我可以得到一致的32个元素,但没有更多。 我猜这是Instagram方面的限制。 The doc says that you can pass a count parameter: On views where pagination ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。