Results 1 to 5 of 5
  1. #1
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Angry 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 ...

  2. #2
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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:
    Java Code:
    try 
    {
    	int i = 0;
    	while (true) 
    	{
    		i++;
    		Thread.sleep(1000);
    		System.out.println("Testing this out! "+i);
    	}
    } catch (InterruptedException e) {
    	e.printStackTrace();
    }
    Unless this gets more complicated down the line, and you have a combination of threads in your program.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: What is wrong with the Code below

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Bahramudin is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: What is wrong with the Code below

    Thank You for your reply ... your suggestion is better

Similar Threads

  1. What is wrong with my code?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 10:44 PM
  2. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 09:30 AM
  3. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 11:26 AM
  4. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •