aspose file tools*
The moose likes Threads and Synchronization and the fly likes Confusion with the call in threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Confusion with the call in threads" Watch "Confusion with the call in threads" New topic
Author

Confusion with the call in threads

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
i wrote a code which goes like this :

It is working but the confusion i'm facing is that :
* the call to is Alive method takes place even before the new thread has started and hence completed,
can anyone explain me the reason???

the output i'm getting is like this :

IS one alive:true
Running in one Value of i=0
Running in one Value of i =1
Running in one Value of i=2

Please help me clear wid this whole stack trace thing


Waiting for enlightenment....
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First off all Use Code Tags. That makes it easier for all of us.

When thread.start() get's called then the thread becomes alive. However there is no guaranty when the run method
will be called. In your case you could see output like this:

Running in one Value of i=0
IS one alive:true
Running in one Value of i =1
Running in one Value of i=2

or

Running in one Value of i=0
Running in one Value of i =1
IS one alive:true
Running in one Value of i=2

or

Running in one Value of i=0
Running in one Value of i =1
Running in one Value of i=2
IS one alive:true


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Maybe using this code it makes more sence

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
i don't get your response, i mean what do you mean when you said "you could see the output",.....
i'm seeing the one and only output i posted,no alternatives......

Kindly help me why is it happening???

when thread is started by writing one.start() a new thread of execution is created,this is sure but either one of following things shud have taken place with uncertainity:
1)it's run method wud have run immediately as soon as i write one.start
in this case "IS one alive:true " shud have been printed anywhere(either in the end or in between sumwhere if the thread moves from running to waiting/blocked/sleep state)
but not in the beginning
2)the main wud get complete and then the new thread's run method wud have run
in this case "IS one alive:true " shud have been printed in the beginning

but always 2) is taking place, why so, please explain
Anil Gautam
Greenhorn

Joined: Apr 15, 2008
Posts: 6
It’s never certain when run() method would be called after calling start() method.
Just add following lines after one.start();
try{
one.join();
}catch(Exception e){}
You will find that SOP output(Alive false) comes in last as main is supposed to wait for one to finish.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
As Wouter said, there is no guarantee whether run will be called right away, or a bit later. That means your code can not rely on this behavior to be one way or another.

However, it does not mean that this behavior is random. It is in fact very likely to be reproducibly the same every time you run the code on the same version of the JVM (which is, after all, a deterministic program). But the behavior could be different on a JVM on a different platform, or even on a different version on the JVM on the same platform.
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
So, it means that it depends on the JVM when a particular thread's run method would run....
In my case, my JVM is always running main first then allowing new thread's run method to execute, but it cannot be generalised, it may vary from machine to machine and precisely saying JVM to JVM.....am i interpretating right,correct me if there are any flaws in the understanding....


i changed the code to sumthing like this :






and the output depicts that the SOP gets printed sumwhere between the loop......like
Running in one Value of i=0
.
.
Running in one Value of i=10
IS one alive:true
Running in one Value of i=11
.
.


that does mean one thing that my JVM is always preferring main thread to run if at all it is in runnable state and not in the waiting/sleep/blocked state.....am i right???
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Common guys
post a reply, help me out.....

it's been a day since last reply has been posted
please help me

i'm about to conclude sumthing important
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
i'm about to conclude sumthing important

So are all of us; what makes your stuff more important than ours?

It's tricky to use qualifications like "always"; I'd be hesitant to draw conclusions about multi-thread behavior from observing a single simple piece of code.
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
i'm not comparing my stuff with yours gentleman
every ranchvisitor's query is equally important,

but since i heard ranchquery holder need not wait for a day to see a reply, i showed my concern

i'm just pleading from evryone to help me out, nothing else

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion with the call in threads