Question About Thread.sleep

Discussion in 'Plugin Development' started by Fluxty, Aug 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Fluxty

    I realize that I can use scheduler programming provided by Bukkit to delay/repeat tasks, but are there times when thread.sleep is OK? I'm currently using it in one of my plugins.

    Code:
    new Thread(d).start();
     
    public class Countdown implements Runnable {
     
        public Player player1 = null;
     
        public void run() {
            try{
                //Converts the countdown value (set in seconds in config) to milliseconds
                Thread.sleep(countdown * 1000);
                arrayList1.remove(player1);
            }catch(Exception ignored){
           
            }
       
        }
    }
    Obviously I removed a lot of the unnecessary code in the above example. The new thread(d) is started in AsyncPlayerChatEvent.
     
  2. Offline

    NoLiver92

    I would not recommend it as it actually stops the thread, this could cause your server to crash or stop players doing tasks (effectively lag), try using a repeating task which allows you to put the delay time in. look at the scheduler tutorial
     
  3. Offline

    blablubbabc

    Yes. If your code is running in a seperate thread, so you are not setting the servers main thread sleeping, it's up to you if you want to use thread.sleep.
     
  4. Offline

    Fluxty

    This definitely sounds pretty n00b, but how do I know when I'm sleeping the main thread or using a separate thread? Does my line "new Thread(d).start();" create a new, separate thread?
     
  5. Offline

    Polaris29

    Yes. What do you think the "new" keyword means?
     
  6. Offline

    Fluxty

    I assumed that was correct, but ya know, I'm just being careful by getting a definite answer from people more experienced than I am. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page