the sequence of a method call and thread state?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   leo_liu
Posted On:   Wednesday, May 15, 2002 12:35 AM

Hi all, I read an artical about thread, and find an interesting problem. public class TestThread3 extends Thread { public void run() { System.out.println("in run() method"); } private void xxx() { System.out.println("in xxx()..."); } public static void main(String[] args) { TestThread3 t3 = new TestThread3(); t3.start(); t3.xxx(); } } result is : in xxx()... in run() method Can anybody tell me why the output of xxx() is exceed run() method?    More>>

Hi all, I read an artical about thread, and find an interesting problem.

			
public class TestThread3 extends Thread
{
public void run()
{
System.out.println("in run() method");
}

private void xxx()
{
System.out.println("in xxx()...");
}

public static void main(String[] args)
{
TestThread3 t3 = new TestThread3();
t3.start();
t3.xxx();
}
}
result is :
in xxx()...
in run() method


Can anybody tell me why the output of xxx() is exceed run() method?    <<Less

Re: the sequence of a method call and thread state?

Posted By:   Lunkwill_Vroomfondel  
Posted On:   Monday, May 20, 2002 03:08 AM

i agree, and this isnt a problem - this is the main aspect of threads: not to be able to run in concurrency!

this means calling the threads start-method produces a concurrent thread to the main-thread.

in this case the main-thread "wins"



you would achieve your "desired" output by not calling the threads start-method, but by calling the threads run-method, because you dont produce a situation of concurrency...



hope it helped a bit.

Re: the sequence of a method call and thread state?

Posted By:   Simon_Ablett  
Posted On:   Wednesday, May 15, 2002 02:53 AM

The output from your 'run' method is displayed as a consequence of your child thread being invoked. The output from the 'xxx' method is displayed as a consequence of your main thread. The main thread is already running and so gets to 'xxx' before the child thread gets into run. Hence 'xxx' is output before 'run'.

Regards.
About | Sitemap | Contact