• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question from Marcus green - Exam1

 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tux extends Thread
{
static String sName = "vandeleur";
public static void main(String argv[]) throws InterruptedException
{
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}

public void piggy(String sName) throws InterruptedException
{
sName = sName + " wiggy";
start();
Thread.sleep(100);
}

public void run()
{
for(int i=0;i < 4; i++)
{
sName = sName + " " + i;
try
{
Thread.sleep(50);
}catch(InterruptedException e)
{}
}
}
}


I don't understand the output. The answer seems to be either "vandeleur" or "Vandeleur 0", Vandeleur 0 1, ...


What I am not clear is what happend to the sname=sname+"wiggy" operation before the thread is spurn off ?

The method call where "start" is encoded belongs to parent thread of new thread ? either way this operation is completely ignored.. Please let me know what's actually happening over here
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was very ignorant.. sorry found out the answer.. just feel bad about starting a new thread and wasting it
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a waste at all. I tried the codes you posted and I believe you need a tr/catch block at the 5 seconds. So the answer is compilation error.

I tried the original question on Marcus exam 1 and not getting what's been suggested. Not sure if its has anything to do with different OS. Tried the same codes on w2K and Linux and getting the same answer.



With the original Marcus codes I got a single "Van" without the thread.sleep. Can you advise on this?

Thanks in advanced,
k
[ September 18, 2004: Message edited by: Kay Liew ]
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result I got too is "Van%". And that's perfectly valid answer. The answer can be anything between "Vandeuler" to "Vandeuler 0 1 2 3 ".

the piggy method manipulates local parameter sname and not the class variables. So the concatenation operation doesn't have any effect on member sname.

Second when the JVM executes the start() statement, it churn out new child Thread. Now there are two possibility. The new thread can be schedule to execute immediately (thus making the parent thread wait) or continue with execution of parent method (piggy). Thus we get the answer.

Java specification maintains that the order of thread executions is undetermined.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a "stinker" of a question, possible more tricky than you will get on the real exam. However, it is very important to know that some thread behaviour is undetermined in Java. Thus whatever happens 100 times on your particular operating system and setup does not guarantee that the same behaviour will happen on some other operating system and setup.
reply
    Bookmark Topic Watch Topic
  • New Topic