Thread: [HOW TO] Add delay's to your code with out sleeping

Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18
  1. #11  
    My TFLOP is better than yours
    Sonic chao's Avatar
    Join Date
    Aug 2011
    Posts
    298
    Thanks given
    51
    Thanks received
    13
    Rep Power
    24
    The problem is that you are dependent on the assumption that it will take exactly one second for the loop to start again. I am honestly not sure if you are trolling or not, but it is bad code design, and I would hate to see a beginner use this approach thinking that it is somehow better and/or more efficient.

    Despite the sleep method being inaccurate or "inefficient", a beginner should go with that when starting out IMO. It is much more intuitive.
    Reply With Quote  
     

  2. #12  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Code:
    long start = System.currentTimeMillis();
    
    while (true) {
        if (System.currentTimeMillis() >= start + 1000) {
            doSomethingAt1Second();
            start = System.currentTimeMillis();
        }
        Thread.sleep(10); // prevent CPU overrun usage
    }
    Also lol @ using nanoTime then erasing the precision. Just use currentTimeMillis boyo

    Better options:
    Threads if no/minimal shared state (implying sleep, allowing for different execution times)
    Scheduled executor service
    Simple sleep()

    your note about about inefficiency is not true, how could sleep() be inefficient exactly? i'll give you a pass because it's clear you're new because you've never found a use for sleep(). do try to be honest with yourself and not create lies to justify you doing something. when you find a real reason to do delayed calls like this, share it and provide a good example.

    i'm sure i could think of a few scenarios where you might be able to use it, but that's not my point. you mention no real world applications. why not share with us why you'd do this opposed to other things? regarding other tutorials, just because someone explains a subject in a way that provides more details you can't find a use for, does not necessarily make it retarded or overly-complicated. you don't cite any resources so it's hard to know exactly what you mean by shutting down any examination.

    all in all, there's not enough substance here to really initiate a meaningful discussion.
    Last edited by funkE; 02-08-2018 at 02:40 AM.
    .
    Reply With Quote  
     

  3. #13  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Honestly no clue what the fuck yall are doing. Creating new threads just to have a small timed task?
    @FunkE's suggestion of Scheduled executor service is the best solution and that's like the only thing you should use out of all the suggestions that have been posted here. It will allow you to schedule tasks with a millisecond precision which is all you'll ever need on a private server; heck majority of the time you should just use the tick-based system anyways, since outgoing/incoming packets etc are all synchronized with ticks (therefore having millisecond precision is unnecessary unless it's completely irrelevant to players).
    Attached image
    Reply With Quote  
     

  4. Thankful users:


  5. #14  
    Banned
    Join Date
    Jan 2018
    Posts
    22
    Thanks given
    5
    Thanks received
    2
    Rep Power
    0
    Quote Originally Posted by Kris View Post
    Honestly no clue what the fuck yall are doing. Creating new threads just to have a small timed task?
    @FunkE's suggestion of Scheduled executor service is the best solution and that's like the only thing you should use out of all the suggestions that have been posted here. It will allow you to schedule tasks with a millisecond precision which is all you'll ever need on a private server; heck majority of the time you should just use the tick-based system anyways, since outgoing/incoming packets etc are all synchronized with ticks (therefore having millisecond precision is unnecessary unless it's completely irrelevant to players).

    that type of code i posted would be a on the main thread of a simple program that has NO created threads, i use it all the time to configure frames per second. it works for me no issues can run hours at a time, so idc what you all say lol you all will gain carpel tunnel before you convince me to use that BS on small code.

    and no i dont assume it runs at exactly 1 second each, but its pretty damn close and there can be delta variables lol.
    Reply With Quote  
     

  6. #15  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by Exiled2D View Post
    that type of code i posted would be a on the main thread of a simple program that has NO created threads, i use it all the time to configure frames per second. it works for me no issues can run hours at a time, so idc what you all say lol you all will gain carpel tunnel before you convince me to use that BS on small code.
    What you have there is just nasty lol, not to mention it's very robust and really not efficient.
    Oh and not to mention your code doesn't work as intended. The hourly and daily sentences are never accessed because the seconds counter is reset to zero after every 60 seconds - the hourly and daily sentences are based off of seconds, not the next variable.
    Attached image
    Reply With Quote  
     

  7. #16  
    Banned
    Join Date
    Jan 2018
    Posts
    22
    Thanks given
    5
    Thanks received
    2
    Rep Power
    0
    Code:
    long startTime;
    long endTime;
    long oneSecond = 1;
    
    while(endTime = startTime-oneSecond) {
    startTime = System.nanoTime()/1000000000;
    
        //DO SOMETHING EVERY SECOND
    
    endTime = System.nanoTime();
    }
    GL setting up frames per second with System.CurrentTimeMillis(); lol.
    the code above using System.nanoTime() seems easier and less abstract
    Reply With Quote  
     

  8. #17  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    Quote Originally Posted by Exiled2D View Post
    Code:
    long startTime;
    long endTime;
    long oneSecond = 1;
    
    while(endTime = startTime-oneSecond) {
    startTime = System.nanoTime()/1000000000;
    
        //DO SOMETHING EVERY SECOND
    
    endTime = System.nanoTime();
    }
    GL setting up frames per second with System.CurrentTimeMillis(); lol.
    the code above using System.nanoTime() seems easier and less abstract
    Dude like I mentioned you're just making this up. You're erasing the precision which makes it the exact same thing as current time currentTimeMillis. You're also causing the processor to kill itself in heat death by not sleeping in the while loop. Read my post...
    Last edited by funkE; 02-09-2018 at 02:21 AM.
    .
    Reply With Quote  
     

  9. #18  
    Renown Programmer
    Bartvh's Avatar
    Join Date
    May 2017
    Posts
    370
    Thanks given
    89
    Thanks received
    208
    Rep Power
    497
    Or you could use Guava's scheduled service: https://google.github.io/guava/relea...edService.html. It takes care of execution time overflow, looks way cleaner/less confusing and is easier to implement. Also often doesn't even add an extra dependency because almost every Java project uses Guava anyway.
    Reply With Quote  
     

Page 2 of 2 FirstFirst 12

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. How To Add Home TeleTab On your EmoteBar!
    By Only in forum Configuration
    Replies: 11
    Last Post: 04-16-2009, 10:02 PM
  2. Replies: 2
    Last Post: 06-12-2008, 12:41 AM
  3. How to add a timer on your server!
    By Damien in forum Tutorials
    Replies: 9
    Last Post: 03-02-2008, 09:03 PM
  4. How to add NPC's to your client!
    By Tzar in forum Tutorials
    Replies: 14
    Last Post: 12-12-2007, 11:32 PM
  5. Replies: 23
    Last Post: 10-20-2007, 05:00 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •