aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question from Marcus green - Exam1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question from Marcus green - Exam1" Watch "Thread question from Marcus green - Exam1" New topic
Author

Thread question from Marcus green - Exam1

Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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

Joined: May 24, 2003
Posts: 425
I was very ignorant.. sorry found out the answer.. just feel bad about starting a new thread and wasting it
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
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 ]

Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question from Marcus green - Exam1