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 12 of 12

Thread: Creating a Thread from a Thread

  1. #1
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Creating a Thread from a Thread

    I have the following code:
            Thread t = new Thread(){
     
                public void run(){
                    System.out.println("I'm called!");
                }
     
            };
     
            for(int i = 0; i < 10; i++){
                t.start();
                t.join();
                t = new Thread(t);
            }

    The question is, why the run method of a thread is invoked only twice (instead of 10 times)?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a Thread from a Thread

    Please post a small, complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Creating a Thread from a Thread

    In fact, this already is a small, complete program. Just wrap the code in the main method and that's it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a Thread from a Thread

    Please post a complete program that compiles, executes and shows the problem. It is important that I have exactly the same code that you are executing so that I can see what the code does when it executes. If I change it, it could be different.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Creating a Thread from a Thread

    OK, here's the full code:

    public class Program {
     
    	public static void main(String[] args) throws InterruptedException {
    		Thread t = new Thread(){
    			public void run(){
    				System.out.println("I was called!");
    			}
    		};
     
    		for(int i = 0; i < 10; i++){
    			t.start();
    			t.join();
    			t = new Thread(t);
    		}
    	}
     
    }

    The problem is that "I was called!" appears only 2 times on the screen. Instead of 10.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a Thread from a Thread

    Look at the output from this code:
    public class Thread_Program {
     
    	public static void main(String[] args) throws InterruptedException {
     
    		Thread t = new Thread(){
    			public void run(){
    				System.out.println("I was called! threadname=" + Thread.currentThread().getName());
    			}
    		};
     
    		for(int i = 0; i < 10; i++){
                           System.out.print(i + " created t= " + t.getName() + " t.run() on next line ");
                            t.run();   // to print msg
    			t.start();
    			t.join();
    			t = new Thread(t);
    		}
    	}
     
    }
    The use of t the first time is with the override of run()
    for the rest of the times, the run() method is the empty default from the Thread class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Creating a Thread from a Thread

    But if I pass a Runnable to a Thread constructor, the Runnable's run() method will be called on thread's start. Thread implements Runnable. Hence, when I say t = new Thread(t) the new thread must run the run method of 't' (who is in the constructor).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating a Thread from a Thread

    new thread must run the run method of 't' (who is in the constructor).
    The first time. The second time t is different.

    Look at the output from the code I posted.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Creating a Thread from a Thread

    Watch your code run in a debugger where you can see what happens. That is probably the best way to "get it" with the code you posted.

  10. #10
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Creating a Thread from a Thread

    Norm, jps, thank you very much! Now I see it. Actually, it becomes very, very obvious in a debugger. So in the first iteration we had a direct call to the target run() method. At the end of this iteration this thread is passed to the standard system thread constructor as a Runnable. In the 2nd iteration the run() on 't' causes the run() on a thread from 1st iteration. In 3rd iteration call to run() on 't' also causes a call to a thread from a previous iteration, and it is a standard system thread with an empty run().

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Thread from a Thread

    There is no such thing as a parent-child relationship between threads in Java. Once created, they have a life of their own.
    Regarding performance, you may want to use an ExecutorService to control the number of threads created in your application. Too many threads will kill performance for sure. See the Executors class too. I sure they will help you....
    The way you are creating threads is perfectly ok if it is only a few. Otherwise, executor services are the preferred method.
    Last edited by copeg; May 29th, 2013 at 09:31 AM. Reason: Removed link

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a Thread from a Thread

    There is no such thing as a parent-child relationship between threads
    a) Technically, there is such a thing via the ThreadGroup class b) what does this have to do with the original posters question? Please stop posting links to spurious non-java doc, non-oracle, 3rd party websites - doing so as a new user, resurrecting several week old posts makes it look very much like self promotion - aka spam. Continue to do so and further action will be taken.

Similar Threads

  1. thread
    By AAAbhijeet in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2013, 01:20 AM
  2. thread
    By AAAbhijeet in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2013, 09:15 AM
  3. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  4. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  5. My First thread
    By shyandeep chatterjee in forum Member Introductions
    Replies: 0
    Last Post: April 18th, 2012, 05:06 AM