领导者在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
injava.util.concurrent
, butleader
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
(usingawait
instead ofawaitNanos
) but I really doubt that. If the new element isn't the head of the queue, then no thread will be signalled. (seeoffer
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 tonull
, 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
最满意答案
您确定仅提供客户端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
-
Instagram帖子网址(Instagram post URL)[2024-02-19]
实际上你正在链接到$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 ...
-
Instagram API响应为空(Instagram API response empty)[2023-07-21]
据我所知,您只会获得沙盒用户的关注者/关注信息。 因此,您可以邀请沙盒用户,开始关注他,然后应该显示它。 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 ...