Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Thread question

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question here:


I thought answer is 2 but it is 4. Shouldn't it be t to call start(), not within piggy()?

Thanks
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "Shouldn't it be t to call start(), not within piggy()?", but I can explain why the answer really is 4.

At first there is just one user thread: the "main" thread, which does whatever main() says to do. When the start() method executes, a second thread is created. This second thread contends for processor time with the main thread. There's no way to predict which thread will do what when. The only guarantee is that they'll both eventually finish. So the main thread might print sName at any time: before, during, or after execution of the second thread's appending code.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by reubin nibuer:
I have a question here:


I thought answer is 2 but it is 4. Shouldn't it be t to call start(), not within piggy()?

Thanks




t is of type tux. Piggy is a method within tux. Calling start from within piggy is the same as calling it on your t object of type tux. It is also the same as saying this.start();
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by reubin nibuer:
I have a question here:

2) Compilation and output of "vandeleur wiggy"
4) Compilation and output of either "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"

I thought answer is 2 but it is 4. Shouldn't it be t to call start(), not within piggy()?

Thanks



I hope you have also noticed that calling piggy( String sName ) will not change the static variable sName outside the method ?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints just "vandeleur". sName is never modified when it is displayed in the main thread.
If an S.O.P were to be inserted inside the run to display sName as well, then it would print
vandeleur
vandeleur 0
vandeleur 0 1
vandeleur 0 1 2
vandeleur 0 1 2 3.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but every time i run the code its only printing vandeleur ?
i run the program 20 times atleast but same result

any idea ?
me using java 1.4.2
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
As you know that there is "Main" thread also running along side your "Tux" Thread.
So sometimes what happens is that before the Tux thread get started i.e. run method is called, the Main thread is already on the continuation and reaches the print statement. So you get only "Vandeleur" printed, even tough the Tux thread is still on the go.

To get the desired output what u can do is, let the Main thread sleep for a while.
In ur main method have this:


Hope its clear
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Philip Heller:
The only guarantee is that they'll both eventually finish. So the main thread might print sName at any time: before, during, or after execution of the second thread's appending code.


This means if the main thread ends before Tux thread' life time, (which print 1,2,3), the Tux thread will end accordingly, since Tux is created by main thread. This also indicates if the parent thread ends, all of its child threads will ends immediately accordingly. Am I right?

static sName value is not changed, which is because piggy method only changes its own parameter variable's value. Thanks for that notice, Soumya.
[ June 08, 2005: Message edited by: reubin nibuer ]
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by reubin nibuer:

This means if the main thread ends before Tux thread' life time, (which print 1,2,3), the Tux thread will end accordingly, since Tux is created by main thread. This also indicates if the parent thread ends, all of its child threads will ends immediately accordingly. Am I right?

static sName value is not changed, which is because piggy method only changes its own parameter variable's value. Thanks for that notice, Soumya.

[ June 08, 2005: Message edited by: reubin nibuer ]



That is not the case. If the threads are not Daemon threads, which in your example they are not, then they have a life of their own. Even if the main thread has completed the Tux thread will still run to completion.
Try putting a print at the end of the Tux threads run method to test this
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could add sleep() to run() and observe that run() returns after the main thread prints sName.

 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this thought

"if the main thread ends before Tux thread' life time, (which print 1,2,3), the Tux thread will end accordingly, since Tux is created by main thread. This also indicates if the parent thread ends, all of its child threads will ends immediately accordingly."

is because I see the correct answer is saying: either "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3". -- the output sometimes does not output all of 0,1,2,3 in case main thread ends before Tux thread.

If the Tux thread always completes its task, the output should always contain vandeleur, 0, 1, 2, 3 with different orders, isn't it? How should it be explained about the case that 0,1,2,3 isn't printed fully, like: "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2".

Thanks
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by reubin nibuer:
I have this thought

"if the main thread ends before Tux thread' life time, (which print 1,2,3), the Tux thread will end accordingly, since Tux is created by main thread. This also indicates if the parent thread ends, all of its child threads will ends immediately accordingly."

is because I see the correct answer is saying: either "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3". -- the output sometimes does not output all of 0,1,2,3 in case main thread ends before Tux thread.

If the Tux thread always completes its task, the output should always contain vandeleur, 0, 1, 2, 3 with different orders, isn't it? How should it be explained about the case that 0,1,2,3 isn't printed fully, like: "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2".

Thanks



This is because your print statement is in the main thread. So since the main thread has ended, even though your String has changed you don't see that printed since the print already executed. This is why I suggest you add an additional print in the Tux thread at the end of the run() so that you can observe this occurring.
[ June 08, 2005: Message edited by: Damien Howard ]
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it eventually , thanks guys.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why i m not seeing the the post well allinged..

i see sometimes many post unallinged... and having horziontal bar too...
i m posting here and not in javaranch forum coz... i m seeing the problem in this post...

i m having resolution of 1024*768 and still big horizontal bar...

why ..in some post i see this problem..

regards
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic