aspose file tools*
The moose likes Beginning Java and the fly likes Help with a Thread Program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with a Thread Program." Watch "Help with a Thread Program." New topic
Author

Help with a Thread Program.

Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Hi Guys,

I came across this code and I could not figure out how it prints from 11 - 20. Could someone please tell me?



The doubt I have is, once the first thread executes from 1 -10 it stops, and when the second thread starts shouldn't it stop immediately, since the condition inside the for loop is set to "<10". How does it go from 11-20?

Any help would be greatly appreciated.

Thanks



VM
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18743
    
  40

Please QuoteYourSources.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Henry Wong wrote:Please QuoteYourSources.



I was practising some java questions from http://www.indiabix.com/online-test/java-programming-test/random, this is where I got this question.

Thanks
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Both thread(a1 and a2) shared the object referenced by 'demo' and have to call its run method when they go from runnable state to running state.

And method called by both are synchronized i.e. at a time only one thread and it has a for loop which during its first iteration declare local loop variable "int i=0".

So, you get your result...see with your keeen eyes that condition is for loop variable and not for an instance variable i.e. count....
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Gaurangkumar Khalasi wrote:Both thread(a1 and a2) shared the object referenced by 'demo' and have to call its run method when they go from runnable state to running state.

And method called by both are synchronized i.e. at a time only one thread and it has a for loop which during its first iteration declare local loop variable "int i=0".

So, you get your result...see with your keeen eyes that condition is for loop variable and not for an instance variable i.e. count....


Thanks a lot...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
One of the threads prints 1‑10 and the other 11‑20.
I suggest you add some debugging to the doSomething() method. Start with System.out.printf("Current thread: %s ", Thread.currentThread().getName()); That sort of thing will allow you to see which thread is calling the method.
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Campbell Ritchie wrote:One of the threads prints 1‑10 and the other 11‑20.
I suggest you add some debugging to the doSomething() method. Start with System.out.printf("Current thread: %s ", Thread.currentThread().getName()); That sort of thing will allow you to see which thread is calling the method.


Thank you Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
You’re welcome
What happened when you tried it? Did you always get the first Thread starting first? What happens if you remove the synchronized keyword? You will find, the nice thing about this sort of exercise is that you can play with it. And you will be pleased about how much you learn by playing with code.
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Campbell Ritchie wrote:You’re welcome
What happened when you tried it? Did you always get the first Thread starting first? What happens if you remove the synchronized keyword? You will find, the nice thing about this sort of exercise is that you can play with it. And you will be pleased about how much you learn by playing with code.


Hi Campbel,

After the first thread was over it said Thread-0, and after the second one it said Thread-1. My initial problem was when I saw the "for loop" I had a condition set in my mind that whatever happens the counter will not got over 10, anways it's all sorted now. Also when I removed the synchronized keyword I found that the numbers were not sequentially printed, you're right these exercises does help a lot.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Well done

More learning: give the threads names, like T1 and T2. Repeat the performance and see whether the one you called T1 always starts first.
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Yup will do.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with a Thread Program.