Java check a specific condition after a thread terminates
I have the following code
PlayerMove move;
Thread t = new Thread(new Timer());
t.start();
move = this.currentPlayer.GetPlayerMove();
how I can check if the move variable is null after the thread t terminates?
java multithreading
add a comment |
I have the following code
PlayerMove move;
Thread t = new Thread(new Timer());
t.start();
move = this.currentPlayer.GetPlayerMove();
how I can check if the move variable is null after the thread t terminates?
java multithreading
1
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
ifcurrentPlayer.GetPlayerMove()
doesn't returnnull
until after the thread ended thenmove
might never be null unless the thread executes so fast that it will end right beforeGetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use aFuture
, join the threads etc.) - what exactly are you trying to achieve?
– Thomas
Nov 20 '18 at 10:53
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03
add a comment |
I have the following code
PlayerMove move;
Thread t = new Thread(new Timer());
t.start();
move = this.currentPlayer.GetPlayerMove();
how I can check if the move variable is null after the thread t terminates?
java multithreading
I have the following code
PlayerMove move;
Thread t = new Thread(new Timer());
t.start();
move = this.currentPlayer.GetPlayerMove();
how I can check if the move variable is null after the thread t terminates?
java multithreading
java multithreading
asked Nov 20 '18 at 10:50
E.OmarE.Omar
1
1
1
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
ifcurrentPlayer.GetPlayerMove()
doesn't returnnull
until after the thread ended thenmove
might never be null unless the thread executes so fast that it will end right beforeGetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use aFuture
, join the threads etc.) - what exactly are you trying to achieve?
– Thomas
Nov 20 '18 at 10:53
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03
add a comment |
1
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
ifcurrentPlayer.GetPlayerMove()
doesn't returnnull
until after the thread ended thenmove
might never be null unless the thread executes so fast that it will end right beforeGetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use aFuture
, join the threads etc.) - what exactly are you trying to achieve?
– Thomas
Nov 20 '18 at 10:53
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03
1
1
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
if
currentPlayer.GetPlayerMove()
doesn't return null
until after the thread ended then move
might never be null unless the thread executes so fast that it will end right before GetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use a Future
, join the threads etc.) - what exactly are you trying to achieve?– Thomas
Nov 20 '18 at 10:53
if
currentPlayer.GetPlayerMove()
doesn't return null
until after the thread ended then move
might never be null unless the thread executes so fast that it will end right before GetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use a Future
, join the threads etc.) - what exactly are you trying to achieve?– Thomas
Nov 20 '18 at 10:53
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03
add a comment |
1 Answer
1
active
oldest
votes
My guess is what you want is something like this:
CompletableFuture<PlayerMove> moveFuture = supplyAsync(currentPlayer::getPlayerMove);
try {
PlayerMove move = moveFuture.get(limit, TimeUnit.MILLISECONDS);
// player moved
} catch (TimeoutException e) {
// player did not move
}
CompletableFuture is an interface representing a result that gets calculated in a separate thread (in this case, a call to currentPlayer.getPlayerMove()). You can retrieve the result by calling get()
which will wait forever or (as in this case), for a specified period of time. If that timeout has elapsed, a TimeoutException
is thrown.
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
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%2f53391367%2fjava-check-a-specific-condition-after-a-thread-terminates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
My guess is what you want is something like this:
CompletableFuture<PlayerMove> moveFuture = supplyAsync(currentPlayer::getPlayerMove);
try {
PlayerMove move = moveFuture.get(limit, TimeUnit.MILLISECONDS);
// player moved
} catch (TimeoutException e) {
// player did not move
}
CompletableFuture is an interface representing a result that gets calculated in a separate thread (in this case, a call to currentPlayer.getPlayerMove()). You can retrieve the result by calling get()
which will wait forever or (as in this case), for a specified period of time. If that timeout has elapsed, a TimeoutException
is thrown.
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
add a comment |
My guess is what you want is something like this:
CompletableFuture<PlayerMove> moveFuture = supplyAsync(currentPlayer::getPlayerMove);
try {
PlayerMove move = moveFuture.get(limit, TimeUnit.MILLISECONDS);
// player moved
} catch (TimeoutException e) {
// player did not move
}
CompletableFuture is an interface representing a result that gets calculated in a separate thread (in this case, a call to currentPlayer.getPlayerMove()). You can retrieve the result by calling get()
which will wait forever or (as in this case), for a specified period of time. If that timeout has elapsed, a TimeoutException
is thrown.
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
add a comment |
My guess is what you want is something like this:
CompletableFuture<PlayerMove> moveFuture = supplyAsync(currentPlayer::getPlayerMove);
try {
PlayerMove move = moveFuture.get(limit, TimeUnit.MILLISECONDS);
// player moved
} catch (TimeoutException e) {
// player did not move
}
CompletableFuture is an interface representing a result that gets calculated in a separate thread (in this case, a call to currentPlayer.getPlayerMove()). You can retrieve the result by calling get()
which will wait forever or (as in this case), for a specified period of time. If that timeout has elapsed, a TimeoutException
is thrown.
My guess is what you want is something like this:
CompletableFuture<PlayerMove> moveFuture = supplyAsync(currentPlayer::getPlayerMove);
try {
PlayerMove move = moveFuture.get(limit, TimeUnit.MILLISECONDS);
// player moved
} catch (TimeoutException e) {
// player did not move
}
CompletableFuture is an interface representing a result that gets calculated in a separate thread (in this case, a call to currentPlayer.getPlayerMove()). You can retrieve the result by calling get()
which will wait forever or (as in this case), for a specified period of time. If that timeout has elapsed, a TimeoutException
is thrown.
edited Nov 20 '18 at 11:47
answered Nov 20 '18 at 11:03


daniudaniu
7,45021635
7,45021635
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
add a comment |
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
Can you explain this solution please?
– E.Omar
Nov 20 '18 at 11:20
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
@E.Omar Added an explanation to the answer.
– daniu
Nov 20 '18 at 11:47
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%2f53391367%2fjava-check-a-specific-condition-after-a-thread-terminates%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
1
It is unclear what you are asking for. Do you want to know how to check if the thread ended? Do you want to know how to write a loop and wait for that method to return a null result?
– GhostCat
Nov 20 '18 at 10:53
if
currentPlayer.GetPlayerMove()
doesn't returnnull
until after the thread ended thenmove
might never be null unless the thread executes so fast that it will end right beforeGetPlayerMove()
is called. Besides that, you'd need to know when the thread has finished and there are a number of ways to do that (e.g. use aFuture
, join the threads etc.) - what exactly are you trying to achieve?– Thomas
Nov 20 '18 at 10:53
@GhostCat I want to know if the move var is null or not after the timer thread ends.
– E.Omar
Nov 20 '18 at 10:57
Are you maybe trying to give the player the opportunity to move for a given time?
– daniu
Nov 20 '18 at 11:00
@Thomas Thanks for your response . What i want to achieve is giving a player a limited time to make a move and if he/she doesn't i wanna stop the game.
– E.Omar
Nov 20 '18 at 11:03