Java Threads

Discussion in 'Java' started by moe247@gmail.com, Apr 29, 2005.

  1. Guest

    Hi - I have a slight issue with threads. I'm also new to Java so
    bear with me if I've done anything stupid!

    There are two main classes: Main and FxThread.

    FxThread extends Thread and implements Runnable and Main creates three
    threads, gets them to run and sticks them in an array with the threads
    running in a "while true" loop.

    Now, the code all works, but it eats the CPU like there is no tomorrow.
    CPU usage actually shoots up to 100% when I run it.

    Why is this?

    Any help would be much appreciated. Thanks in advance!


    ------------------------------
    public class FxThread extends Thread implements Runnable{

    String data;

    FxThread(){
    data = "this is some data";
    }

    public void run(){
    int i = 0;
    while (true)
    {
    i++;
    }
    }
    }
    ------------------------------------
    public static void main (String [] args)
    {

    FxThread [] array = new FxThread[3];

    FxThread thread = new FxThread();
    FxThread thread1 = new FxThread();
    FxThread thread2 = new FxThread();

    thread.start();
    array[0] = thread;

    thread1.start();
    array[1] = thread1;

    thread2.start();
    array[2] = thread2;

    }
    -------------
     
    , Apr 29, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi - I have a slight issue with threads. I'm also new to Java so
    > bear with me if I've done anything stupid!


    comp.lang.java.help is the next door to the right.

    > Now, the code all works, but it eats the CPU like there is no tomorrow.
    > CPU usage actually shoots up to 100% when I run it.


    That is exactly what you have programmed.

    > public void run(){
    > int i = 0;
    > while (true)
    > {
    > i++;
    > }
    > }
    > }


    Here you burn all your CPU cycles. This endless loop runs, and runs, and
    runs, and uses CPU time. What else do you expect?

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Apr 29, 2005
    #2
    1. Advertising

  3. . Guest

    On Fri, 29 Apr 2005 wrote:

    > Hi - I have a slight issue with threads. I'm also new to Java so
    > bear with me if I've done anything stupid!
    >
    > There are two main classes: Main and FxThread.
    >
    > FxThread extends Thread and implements Runnable and Main creates three
    > threads, gets them to run and sticks them in an array with the threads
    > running in a "while true" loop.
    >
    > Now, the code all works, but it eats the CPU like there is no tomorrow.
    > CPU usage actually shoots up to 100% when I run it.
    >
    > Why is this?


    The code in FxThread.run() is pretty straight forward. It does not have to
    wait on anything except the CPU. Programs that run at less than 100% CPU
    are usually waiting for network access, hard drive access, a lock on a
    resource, etc.

    If you want to slow the threads down they will either have to try
    accessing resources shared by others (thus they will have to wait for
    those resources) or you can just add a Thread.sleep(milliseconds); in the
    code to make it pause for a few milliseconds every iteration.

    > Any help would be much appreciated. Thanks in advance!
    >
    >
    > ------------------------------
    > public class FxThread extends Thread implements Runnable{
    >
    > String data;
    >
    > FxThread(){
    > data = "this is some data";
    > }
    >
    > public void run(){
    > int i = 0;
    > while (true)
    > {
    > i++;


    Thread.sleep(1000); // sleep for a second

    > }
    > }
    > }
    > ------------------------------------
    > public static void main (String [] args)
    > {
    >
    > FxThread [] array = new FxThread[3];
    >
    > FxThread thread = new FxThread();
    > FxThread thread1 = new FxThread();
    > FxThread thread2 = new FxThread();
    >
    > thread.start();
    > array[0] = thread;
    >
    > thread1.start();
    > array[1] = thread1;
    >
    > thread2.start();
    > array[2] = thread2;
    > }
    > -------------


    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
     
    ., Apr 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Renato Battaglia
    Replies:
    3
    Views:
    2,842
    Jon Skeet
    Oct 24, 2003
  2. yoda
    Replies:
    2
    Views:
    480
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    448
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,521
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    187
    Une bévue
    Jun 14, 2006
Loading...

Share This Page