jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Threads 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 "Doubt in Threads" Watch "Doubt in Threads" New topic

Doubt in Threads

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Apr 02, 2000
Posts: 65
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.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Read this post for more details.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sandy L

Joined: Oct 20, 2000
Posts: 9
This is my new registration. I am called sandy
by my friends and I very much prefer it.
Thanks Ajit
Gayatri Deshpande

Joined: Oct 15, 2000
Posts: 11
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.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1868
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.

Rahul P. Mahindrakar
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
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.

I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt in Threads
jQuery in Action, 3rd edition