Valhalla Legends Forums Archive | Java Programming | Infinite loop

AuthorMessageTime
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:

[code]
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++;
      }
[/code]

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.
January 22, 2004, 5:25 AM
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).
January 22, 2004, 12:35 PM
iago
Here is how it's done in O'Reilly's book on Servlets:

[code]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)
      {
      }
   }
}[/code]
January 22, 2004, 2:06 PM
Tuberload
[quote author=iago link=board=34;threadid=4841;start=0#msg40609 date=1074774920]
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).
[/quote]

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.
January 22, 2004, 8:06 PM
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.
January 22, 2004, 11:58 PM
Kp
[quote author=Orillion link=board=34;threadid=4841;start=0#msg40737 date=1074815913]
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.[/quote]

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.
January 23, 2004, 12:05 AM

Search