• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with a Thread Program.

 
Vineeth Menon
Ranch Hand
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Vineeth Menon
Ranch Hand
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup will do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic