• Welcome to Valhalla Legends Archive.
 

Infinite loop

Started by Tuberload, January 21, 2004, 11:25 PM

Previous topic - Next topic

Tuberload

At the base of my bot I use an infinite loop to handle incoming and outgoing data, and various other things. The code is as follows:


while (true)
      {
         if (t_sock.available())
            generator.execute (t_sock.recv(), m_list, c_handle, t_sock);
         t_sock.send();
         
         // TODO: Find out if the following line is necesary, and
         // what kind of effects the program has on the computer if it uses
         // 100% of CPU compared to 1%.
         Thread.sleep (1); // Dramatically decreases the CPU usage
         
         // Place any timed events in the following
         if (count > 10000)
         {
            settings.saveSettings();
            count = 0;
         }
         else
            count++;
      }


When I place the Thread.sleep(1) into the loop it drops the cpu usage to next to nothing, but without it the bot hogs 90-99%. I am just curious what kind of effects this could have on a computer? I am also wondering if there is a better to do what my code does?

Edit: My timed functions will not work properly without putting the thread to sleep, so I guess my main question is whether or not there is a better way to accomplish this.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

iago

Putting the thread to sleep gives up the rest of its cpu time and allows other threads to use the cpu.  It's mainly a thoughtful thing to do.

One other thing you should do is make that low priority.  I can't remember how to do that, though, you'll have to look that up or wait a few hours (I have it in a .pdf on linux at work, but I'm at home right now).
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


iago

#2
Here is how it's done in O'Reilly's book on Servlets:

public void init(ServletConfig config) throws ServletException
{
   super.init(config); // always!
   searcher = new Thread(this);
   searcher.setPriority(Thread.MIN_PRIORITY); // be a good citizen
   searcher.start();
}
// [.....]
public void run()
{
   //               QTTTBBBMMMTTTOOO
   long candidate = 1000000000000001L; // one quadrillion and one
   // Begin loop searching for primes
   while (true)
   { // search forever
      if (isPrime(candidate))
      {
         lastprime = candidate; // new prime
         lastprimeModified = new Date(); // new "prime time"
      }
      candidate += 2; // evens aren't prime
      // Between candidates take a 0.2 second break.
      // Another way to be a good citizen with system resources.
      try
      {
         searcher.sleep(200);
      }
      catch (InterruptedException ignored)
      {
      }
   }
}
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Tuberload

Quote from: iago on January 22, 2004, 06:35 AM
Putting the thread to sleep gives up the rest of its cpu time and allows other threads to use the cpu.  It's mainly a thoughtful thing to do.

One other thing you should do is make that low priority.  I can't remember how to do that, though, you'll have to look that up or wait a few hours (I have it in a .pdf on linux at work, but I'm at home right now).

Yes I know what putting the thread to sleep does, as well as how to set it to low priority. I was just wondering if how I am doing it is good or not.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Orillion

There would not be much need to put it to sleep like you do unless the program really is very intensive and/or your wanting to use some other intensive software at the same time. If your talking just a normal bot, I dont see much reason to put it to sleep like that.

Kp

Quote from: Orillion on January 22, 2004, 05:58 PM
There would not be much need to put it to sleep like you do unless the program really is very intensive and/or your wanting to use some other intensive software at the same time. If your talking just a normal bot, I dont see much reason to put it to sleep like that.

Perhaps you missed his explanation of why he has it sleeping?  Since he isn't taking any other blocking actions, that sleep call is the only thing that yields up the thread's time slice.  If not for that, it'd run at full CPU constantly -- a serious annoyance to most computer users.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!