Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion with the call in threads

 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe using this code it makes more sence

 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic