How to delay an if-statement check? Java/Eclipse

Soldato
Joined
15 Aug 2010
Posts
8,753
Location
N. Ireland
I am just wondering if there is anyway to delay an if-statement check? The condition for the if-statement requires an input value from another class and every time I run the code, it always checks the condition before I can input the value and save it to the variable.
 
Caporegime
Joined
18 Oct 2002
Posts
32,618
I'm kind of confused about what you are saying. In java, in a single threaded application the code will always follow a deterministic, in-order execution path.

It sounds like you have a multi-threaded application, in which case things can happen out of order.

E.g if SpwanThread starts some thread to compute someFunction(...) in th background then result wont be initiilized by the time the if statement is processed.
Code:
int result = SpawnThread(someFunction(someInputValue) );
if (result == 0)
{
   doSomething...
}



You need to look into mutexes, locks, detached and joined threads.
 
Soldato
Joined
18 Aug 2011
Posts
2,852
Location
Norfolk
Something like:

synchronized public void depart()throws InterruptedException
{
while (spaces.getCount()== capacity)
{
this.wait();
}
spaces.increaseByOne();
System.out.println("Departed -free spaces = "+ spaces.getCount());
this.notifyAll();
}

While the test remains X do nothing else do whatever and notify other threads.

http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep

Might help?
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Yeah, I believe I am multi-threading.
We haven't looked at any of those things yet and I have no knowledge with them :(

I assume this is for some sort of school/university work?

What makes you think you're multi-threading if you haven't looked at that yet and have no knowledge of it?

Can you post a bit more of your code so we can see what's going on?
 
Soldato
OP
Joined
15 Aug 2010
Posts
8,753
Location
N. Ireland
I assume this is for some sort of school/university work?

What makes you think you're multi-threading if you haven't looked at that yet and have no knowledge of it?

Can you post a bit more of your code so we can see what's going on?

Yup, uni assignment, it just came up as one of the suggestions when googling :p

Make the class that provides the input a Callable and use the returned Future in your if statement. The thread will block until the input is made available.

I got it sorted somehow by thread sleeping only to be told by a friend that its basically "cheating" but I had to submit it yesterday, and while its "cheating" it got the code to work as I wanted.

But i'll definitely look into other ways of doing it in the future.
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
I got it sorted somehow by thread sleeping only to be told by a friend that its basically "cheating" but I had to submit it yesterday, and while its "cheating" it got the code to work as I wanted.

But i'll definitely look into other ways of doing it in the future.

The problem with doing it via a thread sleep is that it keeps a thread tied up for the length of time you're sleeping.
Plus you have no idea how long to actually sleep for. If it is multi-threading that's causing the issue you can't tell when the other thread has finished its stuff just by sleeping for a while.
It may have finished by the time you stop sleeping and everything works, but it may not.

The only way to be sure is to have the worker thread notify you when it's completed, which something like Inferno99's suggestion lets you do.

Having said all that I find it hard to believe you've inadvertently introduced some multi-threading stuff in a uni assignment if you don't know anything about it.
 

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
Can you post a bit of what you've got and what's not working? We might be able to help you get a better solution if we can see something.

^^ This. Most people on here are willing to help but you need to give more information rather than trying to guess what question to ask without really understanding the problem you're having. Can you post more code, or the question/specification?

I got it sorted somehow by thread sleeping only to be told by a friend that its basically "cheating" but I had to submit it yesterday, and while its "cheating" it got the code to work as I wanted.

Friend is right, and if solving this issue is part of the assignment I wouldn't count on a great mark for sleeping the thread...!
 
Back
Top Bottom