M
moe247
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;
}
-------------
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;
}
-------------