How to know if another thread does SynchronousQueue poll?
I have very obscure use case but in essence I need to know if another thread does SynchronousQueue poll
(with timeout) and it it does I want to insert item and unblock it.
Is there any straightforward way to do that? From my naive read code of javadoc and code it seems like no but I thought I would check wiht SOE.
java asynchronous java.util.concurrent thread-synchronization
add a comment |
I have very obscure use case but in essence I need to know if another thread does SynchronousQueue poll
(with timeout) and it it does I want to insert item and unblock it.
Is there any straightforward way to do that? From my naive read code of javadoc and code it seems like no but I thought I would check wiht SOE.
java asynchronous java.util.concurrent thread-synchronization
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@bl3e at point where I want to do something another thread certainly in blocked state (eg. calledpoll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).
– Petro Semeniuk
Dec 16 '18 at 13:35
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23
add a comment |
I have very obscure use case but in essence I need to know if another thread does SynchronousQueue poll
(with timeout) and it it does I want to insert item and unblock it.
Is there any straightforward way to do that? From my naive read code of javadoc and code it seems like no but I thought I would check wiht SOE.
java asynchronous java.util.concurrent thread-synchronization
I have very obscure use case but in essence I need to know if another thread does SynchronousQueue poll
(with timeout) and it it does I want to insert item and unblock it.
Is there any straightforward way to do that? From my naive read code of javadoc and code it seems like no but I thought I would check wiht SOE.
java asynchronous java.util.concurrent thread-synchronization
java asynchronous java.util.concurrent thread-synchronization
edited Dec 16 '18 at 13:28
Petro Semeniuk
asked Dec 16 '18 at 13:07
Petro SemeniukPetro Semeniuk
4,91033455
4,91033455
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@bl3e at point where I want to do something another thread certainly in blocked state (eg. calledpoll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).
– Petro Semeniuk
Dec 16 '18 at 13:35
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23
add a comment |
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@bl3e at point where I want to do something another thread certainly in blocked state (eg. calledpoll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).
– Petro Semeniuk
Dec 16 '18 at 13:35
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@bl3e at point where I want to do something another thread certainly in blocked state (eg. called
poll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).– Petro Semeniuk
Dec 16 '18 at 13:35
@bl3e at point where I want to do something another thread certainly in blocked state (eg. called
poll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).– Petro Semeniuk
Dec 16 '18 at 13:35
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23
add a comment |
2 Answers
2
active
oldest
votes
What if you implemented this logic and your producer thread adds value to the queue each time it finds out there is another thread blocked on poll
- where is guarantee that the value you producer puts into queue will be consumed by that thread and not another ?
Suggest to change you design. Producer - the thread that adds values to queue - should know nothing about consumers. Make you produces a request consumer. I mean define another queue and make your current thread that makes poll
submits a request
. And you current producer thread takes requests from that new queue and fulfils request. You can implement request object the way that it works as mediator between two threads.
add a comment |
So after number of solutions (custom queue, separate volatile variable to track insertion) it ended up very simple solution with using offer
method:
/**
* Inserts the specified element into this queue, if another thread is
* waiting to receive it.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}
If I just do if(offer(e)) ... else ...
that helps unblock consumer if any.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53802442%2fhow-to-know-if-another-thread-does-synchronousqueue-poll%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What if you implemented this logic and your producer thread adds value to the queue each time it finds out there is another thread blocked on poll
- where is guarantee that the value you producer puts into queue will be consumed by that thread and not another ?
Suggest to change you design. Producer - the thread that adds values to queue - should know nothing about consumers. Make you produces a request consumer. I mean define another queue and make your current thread that makes poll
submits a request
. And you current producer thread takes requests from that new queue and fulfils request. You can implement request object the way that it works as mediator between two threads.
add a comment |
What if you implemented this logic and your producer thread adds value to the queue each time it finds out there is another thread blocked on poll
- where is guarantee that the value you producer puts into queue will be consumed by that thread and not another ?
Suggest to change you design. Producer - the thread that adds values to queue - should know nothing about consumers. Make you produces a request consumer. I mean define another queue and make your current thread that makes poll
submits a request
. And you current producer thread takes requests from that new queue and fulfils request. You can implement request object the way that it works as mediator between two threads.
add a comment |
What if you implemented this logic and your producer thread adds value to the queue each time it finds out there is another thread blocked on poll
- where is guarantee that the value you producer puts into queue will be consumed by that thread and not another ?
Suggest to change you design. Producer - the thread that adds values to queue - should know nothing about consumers. Make you produces a request consumer. I mean define another queue and make your current thread that makes poll
submits a request
. And you current producer thread takes requests from that new queue and fulfils request. You can implement request object the way that it works as mediator between two threads.
What if you implemented this logic and your producer thread adds value to the queue each time it finds out there is another thread blocked on poll
- where is guarantee that the value you producer puts into queue will be consumed by that thread and not another ?
Suggest to change you design. Producer - the thread that adds values to queue - should know nothing about consumers. Make you produces a request consumer. I mean define another queue and make your current thread that makes poll
submits a request
. And you current producer thread takes requests from that new queue and fulfils request. You can implement request object the way that it works as mediator between two threads.
answered Dec 16 '18 at 13:41
Oleksandr PapchenkoOleksandr Papchenko
881822
881822
add a comment |
add a comment |
So after number of solutions (custom queue, separate volatile variable to track insertion) it ended up very simple solution with using offer
method:
/**
* Inserts the specified element into this queue, if another thread is
* waiting to receive it.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}
If I just do if(offer(e)) ... else ...
that helps unblock consumer if any.
add a comment |
So after number of solutions (custom queue, separate volatile variable to track insertion) it ended up very simple solution with using offer
method:
/**
* Inserts the specified element into this queue, if another thread is
* waiting to receive it.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}
If I just do if(offer(e)) ... else ...
that helps unblock consumer if any.
add a comment |
So after number of solutions (custom queue, separate volatile variable to track insertion) it ended up very simple solution with using offer
method:
/**
* Inserts the specified element into this queue, if another thread is
* waiting to receive it.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}
If I just do if(offer(e)) ... else ...
that helps unblock consumer if any.
So after number of solutions (custom queue, separate volatile variable to track insertion) it ended up very simple solution with using offer
method:
/**
* Inserts the specified element into this queue, if another thread is
* waiting to receive it.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}
If I just do if(offer(e)) ... else ...
that helps unblock consumer if any.
answered Jan 1 at 3:21
Petro SemeniukPetro Semeniuk
4,91033455
4,91033455
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53802442%2fhow-to-know-if-another-thread-does-synchronousqueue-poll%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you can extend SynchronousQueue and override the poll(long timeout, TimeUnit unit) method to do whatever you want and call the super class method
– bl3e
Dec 16 '18 at 13:21
@孙兴斌 I should have clarified - another thread does poll with timeout.
– Petro Semeniuk
Dec 16 '18 at 13:29
@bl3e at point where I want to do something another thread certainly in blocked state (eg. called
poll
) and I can do little with override is super.poll already called (technically I can do thread interrupt but I would rather not).– Petro Semeniuk
Dec 16 '18 at 13:35
If you could use a subclass then the thread calling poll of the inherited class(say NotifyOnPollSynchronousQueue) won't go in blocking state until it calls poll of it's super class ,i.e SynchronousQueue
– bl3e
Dec 16 '18 at 16:13
@bl3e I had working solution with subclass but it ended up very simple solution with build-in method from SQ - please take a look into my answer.
– Petro Semeniuk
Jan 1 at 3:23