File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Waiting in a method without creation of threads

 
prathik mr
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
prathik mr
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Peter Taucher
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic