Having a main thread that runs continuously?

Discussion in 'Plugin Development' started by gus, Jun 8, 2012.

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

    gus

    i know this isn't strictly bukkit-related, but this is a serious gap in my java knowledge that i need to fill.

    so what i have is a program (not a plugin) that listens in on a port to data sent from a plugin. so far i've successfully connected the two together and transmitted data. the problem is, though, once a connection is made and the data is sent, the main thread terminates, when i want it to go back and listen for another connection, or possibly be able to accept multiple connections at a time.

    i thought about using a while(1==0) loop or something like that, but there must be a real way to do this. like, how do bukkit listeners work? how do they constantly listen without terminating a thread?
     
  2. Offline

    Superkabii

    You're probably going to have more threads to accomplish what you want. The main thread would create the connection listener thread, which would create a connection handler instance for the specific connections whenever one is made.

    On the topic of Bukkit listeners, they don't "listen" persay. The methods you register are fired when the appropriate events occur in the server thread.
     
    gus likes this.
  3. Offline

    V10lator

    Well, 1==0 will always return false, so it isn't but you want... but yes, your program needs a so called "main loop". Simplest case: while(true) ... better:
    Code:java
    1. boolean running = true;
    2. while(running)
    3. {
    4. //main loop...
    5. }

    Bukkit listeners work a completely different way: They are (as the name states) listening... But they wouldn't work if minecraft itself wouldn't call the events from within it's main loop...

    Long story short: You're lacking basic programmer knowledge. Go learn more... ;)
     
    gus likes this.
  4. Offline

    Digi

    Wait... aren't loops and threads 2 diferent things ?

    As far as I know:
    Threads are codes ran from top to bottom over and over again and each thread takes as long as the code takes to finish... then another cycle (if requested) and so on...
    Loops (like while(), for(), etc) makes the code inside it repeat until it finishes and then it goes to the next code... all that happens in a single thread cycle.

    So basicall a while(true) makes something repeat really fast but it basically makes the program not respond anymore until it finishes the loop... again, as far as I know, so please correct me if I'm wrong :}
     
  5. Offline

    V10lator

    sure, but he wanted to write a program, not a bukkit plugin. A program needs (not every time ;)) a main loop.
    No, they run from top to bottom end exit... That's why you need the main loop... ;)
     
  6. Offline

    gus

    yeah i meant 1==1 haha...

    what would change "running" to false, though? a command in the console, i guess? that would make sense...

    and i know i have gaps in my knowledge, but when you say go learn more...that's what i'm doing right now, haha. thanks for the help so far!
     
  7. Offline

    V10lator

    Whatever you want... if(whateverAction()) running = false;
    ;)
    You're even allowed to set running to false from other functions, classes, ...
     
  8. Offline

    gus

    ah ok, well i'm gonna try to make it so a command in console will stop it.
     
  9. Offline

    V10lator

    gus Wait, what? I thought you're developing a program, not a plugin! If you're developing plugin which starts a new thread make sure to read (and understand!) this: http://wiki.bukkit.org/Scheduler_Programming as handling a command from minecrafts main thread to your new thread is all other than easy...
     
  10. Offline

    gus

    nah, it's a program, not a plugin.
     
  11. Offline

    Digi

    Oh... duh! Yes, when the program thread ended the program IS ended =) And I guess this applies for all similar languages (C and suff).
     
Thread Status:
Not open for further replies.

Share This Page