Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Tell a java method to wait

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Tell a java method to wait

    I want to tell my java method to wait one second while something else is happening. Why not use Thread.sleep(x)? For me it stops the whole program and does not let that some else happen. Why not use wait(x)? It crashes. I tried using timers, but that doesn't solve the problem... Isn't there a simple "Java please wait 1 sec for him to catch up"?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell a java method to wait

    wait 1 sec for him to catch up
    What event do you want to wait for?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tell a java method to wait

    I want to to wait for an object to "drop". It takes about half a second for an Object to "Drop".

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Tell a java method to wait

    How many threads are running? Is your program a console program, a Swing program, or something else?

    If you sleep the only thread running, then you'll see the behavior you describe. If you sleep one thread while another is running, then you may get the desired results.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tell a java method to wait

    It's actually a mod for Minecraft.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell a java method to wait

    I assume you are talking about some shape drawn on the GUI. If not, please explain.
    What thread is the method that you want to wait executing on? If its the EDT then you don't want to hold up that thread.
    One way would be to split the method at the point its to wait into two parts and use a Timer at the end of the first part to do the waiting and then start the second part of the "method".
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tell a java method to wait

    Correction: I don't wan't the method to wait, I the method to be delayed. Sorry about that :/

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell a java method to wait

    Please explain the difference.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tell a java method to wait

    I don't want the whole game to wait I just want a method to be delayed for one second, so the object can drop and then get it's location.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell a java method to wait

    The Timer class would start a method at any later time you want.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tell a java method to wait

    But the method is in an Event and you can't put an Event in an Event...
    Nvm I'm not explaining this correctly.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tell a java method to wait

    I have no idea what you mean by: the method is in an Event
    There is a java SE class named Event.

    You need to make a small, simple, complete program that compiles and executes for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Tell a java method to wait

    I think the OP is trying to mod a game but does not quite know how the games internals work. If this is the case I doubt that anybody here can really help you.

Similar Threads

  1. wait() and notify()
    By gautammuktsar@gmail.com in forum Threads
    Replies: 8
    Last Post: March 22nd, 2018, 06:00 AM
  2. wait(); and notify();
    By tenacious23cb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2013, 04:36 PM
  3. wait(), sleep() and notify()
    By evthim in forum Threads
    Replies: 3
    Last Post: October 30th, 2012, 11:37 AM
  4. let first thread through, the rest has to wait
    By hamsterofdeath in forum Threads
    Replies: 0
    Last Post: November 19th, 2010, 01:32 PM
  5. Delay/Wait/Pause in Java + AirBrushing
    By obliza in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2009, 10:27 AM

Tags for this Thread