Results 1 to 5 of 5
- 08-16-2012, 05:57 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
What is wrong with the Code below
Hi friends, i have a problem with using the "Thread Class" it is when i run the program, the CPU usage goes to very high level, the code which i wrote is:
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 100; i++) {
Thread.sleep(1000);
//doing something here
if (i == 99)
i = 0;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
now the problem is if i don't use the for loop, then the Thread stop working and not running the specified code in every one second, i want to every one second run the specified code. if there is any way to do this without for loop, please help me give an Example ...
- 08-16-2012, 09:51 AM #2
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: What is wrong with the Code below
Hello
I have solve the problem, it is use two while() loops will not happen this problem, the code is below:
but i don't know it is OK or not, if any one know better then this way please reply the thread thanks
new Thread(new Runnable() {
@Override
public void run() {
try {
whie(true) {
Thread.sleep(1000);
while(isTrue) {
Thread.sleep(1000);
//doing something here
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();Last edited by Bahramudin; 08-16-2012 at 09:54 AM.
- 08-16-2012, 03:49 PM #3
Re: What is wrong with the Code below
Nice to see that you fixed your problem. I tried your code before and it seemed fine to me. My only question is, why not do something like this:
Unless this gets more complicated down the line, and you have a combination of threads in your program.Java Code:try { int i = 0; while (true) { i++; Thread.sleep(1000); System.out.println("Testing this out! "+i); } } catch (InterruptedException e) { e.printStackTrace(); }My API:Java Code:cat > a.out || cat > main.class
- 08-16-2012, 04:05 PM #4
Re: What is wrong with the Code below
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-16-2012, 07:10 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
What is wrong with my code?
By Solarsonic in forum New To JavaReplies: 3Last Post: 03-22-2011, 10:44 PM -
what is wrong in dis code?
By jitun2004 in forum New To JavaReplies: 8Last Post: 04-15-2009, 09:30 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks