This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Waiting in a method without creation of threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Waiting in a method without creation of threads" Watch "Waiting in a method without creation of threads" New topic
Author

Waiting in a method without creation of threads

prathik mr
Greenhorn

Joined: Jun 18, 2010
Posts: 4
Hi All

I have simple requirment i just want a wait inside a method for specific duration of time.

i tried Thread.sleep(<duration>); wait(); Thread.currentThread.wait() but none of these is working.
i want to achieve this using without Threads. Any inputs would be appriciated.

I am implementing the hardware callbacks for which i am having the above requirement.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
prathik rajalbandi wrote:but none of these is working.

ItDoesntWorkIsUseless so please TellTheDetails. The sleep() method of class Thread always worked in my scenarios and I can't comprehend, why you would say, it didn't work for you...


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
prathik mr
Greenhorn

Joined: Jun 18, 2010
Posts: 4


Thread.sleep takes argument as no of miliseconds. During program execution my execution is not suspended.

I want to wait till the value is setby the callback. So please correct me if i cant use sleep in this scenario. Any inputs would be helpful
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
I really can't comprehend your 'scenario'. Why don't you create a SSCCE, or at least PostRealCode. Crystal balls are so out this season... ShowSomeEffort!
prathik mr
Greenhorn

Joined: Jun 18, 2010
Posts: 4
Peter Taucher wrote:I really can't comprehend your 'scenario'. Why don't you create a SSCCE, or at least PostRealCode. Crystal balls are so out this season... ShowSomeEffort!


String serverIP = null;
public requestIp() {
Client.requestForIp(Isp, handler)
Thread.sleep(10000);
// I am not getting expected behaviour if i use Thread.sleep
/** here i want to wait till the serverIp is set by the handler **/
Client.requestForSever(serverIp, handler)
}

public method handler (response) {
serverIp = response.getServerIP();
}

I hope this gives some picture of what i need.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Please use code tags next time.

No, that's not enough info. Who calls which methods? You simply wait 10 seconds, you do not wait until some (external?) event was processed. How shall we determine the coherences of your code / problem? Do you really not understand that we need much more info in order to help you? Is it impossible to narrow down your problems to a SSCCE? Is that too much to ask for? Do you really need our help? I doubt it (or you would have provided more infos)...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

prathik rajalbandi wrote:
Peter Taucher wrote:I really can't comprehend your 'scenario'. Why don't you create a SSCCE, or at least PostRealCode. Crystal balls are so out this season... ShowSomeEffort!


String serverIP = null;
public requestIp() {
Client.requestForIp(Isp, handler)
Thread.sleep(10000);
// I am not getting expected behaviour if i use Thread.sleep
/** here i want to wait till the serverIp is set by the handler **/
Client.requestForSever(serverIp, handler)
}

public method handler (response) {
serverIp = response.getServerIP();
}

I hope this gives some picture of what i need.


A bunch of things wrong here... but let's start with the biggest:

First, it looks like you are trying to pass a method as a parameter. You can't do this in Java because methods aren't Objects. This being said, not much of your code makes any sense.

Now, let's assume that you have some object which does behave the way you want (probably some implementation of an interface). You want to pause execution of the application, mid-method, and wait for other code to be executed. If you 'don't use threads' how do you expect the 'other code' which sets serverIp to be executed? It can't because there is just one stream of execution (one Thread) and you stop executing it. So theoretically there is no way to do what you want.

So you have to understand your requirements better. What do you want to do, and what tools do you need to do it. This is why Peter is talking about SSCCE. If you haven't done so, follow the links that appear each time we type SSCCE. It is a good resource and will explain what we need to help you (and in the process maybe you will see what you need yourself).


Steve
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Steve Luke wrote:... not much of your code makes any sense.

But from his postings you could get the impression that we were talking about already running code (and not uncompilable jumble).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Waiting in a method without creation of threads