Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Thread runs only once and not again

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Thread runs only once and not again

    I have something like the following:

    class t_thread extends Thread {
     
            public t_thread() {
                new Thread(this).start();
            }
     
            @Override
            public void run() {
                // change some variables
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
            }
    }

    And I call it like:

    t_thread t = new t_thread();

    The problem is that, it runs once when it is created, sleeps, and never runs again. If I add a run() after sleep(1000), it works fine but eventually causes a Stack Overflow.

    Does anyone know why it does not continue to run until I stop it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Thread runs only once and not again

    why it does not continue to run
    Threads "run" until they exit the run() method. Your code exits immediately after sleeping.
    Why not use a loop in the run method if you want that thread to "run" again.

  3. The Following User Says Thank You to Norm For This Useful Post:

    enflation (June 3rd, 2010)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Thread runs only once and not again

    Something like

            @Override
            public void run() {
                while(true) {
                    // change some variables
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {}
                }
            }

    You might want to think of a way you can interrupt this though, but it gives you a clue on how to do it.

    // Json

  5. #4
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: Thread runs only once and not again

    Probably a better way so that it can get interrupted

    @Override
            public void run() {
                try {
                    while(true) {
                         // change some variables
                         Thread.sleep(1000);
                    } catch (InterruptedException e) {}
                }
            }

Similar Threads

  1. Blackjack program runs excrutiatingly slow.
    By sina12345 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2010, 04:55 AM
  2. Is thread the best option?
    By 256mxr in forum Threads
    Replies: 1
    Last Post: May 22nd, 2010, 08:24 PM
  3. Thread and connection
    By Param in forum Threads
    Replies: 0
    Last Post: April 26th, 2010, 03:43 AM
  4. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 06:23 PM
  5. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM

Tags for this Thread