• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Threads

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
Why does the program produce Thread : 3 all the three times?
public class Basics extends Thread{
public static int i;
public static void main( String []args){
for(i=0; i<3; i++){<br /> Basics sweater = new Basics();<br /> sweater.start();<br /> }<br /> }<br /> public void run(){<br /> System.out.println(" Thread : " + i);<br /> }<br /> }<br /> --->Sandy
 
Aparanji Raju
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sandy,
1.the variable i is a static variable which belongs to the class.
2.Further,in the for loop,you are creating three threads and invoking start() for each of them in the for loop itself.
3.Having created three threads,there is a copy of the static variable i in each of the three threads which is incremented everytime there is an iteration and a call to start in the for loop.
Hope this helps.Somebody,please correct me if i am wrong.
Aparanji.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Sandy',
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
Sandy L
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my new registration. I am called sandy
by my friends and I very much prefer it.
Thanks Ajit
 
Gayatri Deshpande
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aparanji,you're right.
As i is a static variable,its value changes as you traverse through the For loop which is executed first.Then the run method is executed for all the three threads created.
And hence you see 3 three times.
Hope this helps.
Gayatri
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have changed the code a little bit to increase the loops.

Now what happens is that new threads are being created in the loop and the value of the variable i is also being changed regularly. The new threads created will take the current value of the variable i and print this out.
In my system the loop stops at 1865!! saying that exception in thread. Is this the behaviour in other's systems?? If so does this 1865 have some significance.


------------------
Regds.
Rahul P. Mahindrakar
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my compilation.It runs for 2000 times.I thing your program is
making to much string pool for System.
System.out.println("Thread : " + i);
and it might be giving exception due to the same.
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.whenever you call thread() start method the thread is in
ready state but don't start's executing immediately.
2.Increase it to some significant value at which it will be
with all probablility will start running at mulitiple time
say i <300.You may get 153,300 and may be one more value.
3.When thread's run method actually gets started it verify class staic value i and prints it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic