• Post Reply Bookmark Topic Watch Topic
  • New Topic

take a look at this:  RSS feed

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it compiles, runs, until u force it's fianlization!!! the strange thing is that it seems that every thread has a local copy of the variable i, for example, th1 increments its variable i to 20 than it goes to sleep, after, maybe, th2 wakes and get the lock on the object, shouldn't it increment the value of i to 21???
but it uses the last value it used!!! please, run it, explain me why am I seing this result!!!
thanks for all help!!!
[added [code] tags - Jim]
[ February 17, 2003: Message edited by: Jim Yingst ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

th1 increments its variable i to 20 than it goes to sleep, after, maybe, th2 wakes and get the lock on the object, shouldn't it increment the value of i to 21???

In your code, the two threads that you start are the instances of two different classes. Class th1 has its own member variable i, and class th2 has its own member variable i. There is no relationship between these two member variables whatsoever, they are completely independent. So when you increment the variable that belongs to class th1, there is no effect on the member variable i that belongs to class th2.
Perhaps what you really intended to do is to see what would happen if you run two threads that are instances of the same class. Another educational demo would be to keep and increment variable i inside your SomeObject class.
In the future, please use the "code" tag when you post your code, -- it makes it much more readable. Also, when giving a subject to your post, use something meaningful. When the subject line is "Take a look at this", the reasonable response you may expect is "Why should I?".
Eugene.
[ February 17, 2003: Message edited by: Eugene Kononov ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leandro,

it compiles, runs, until u force it's fianlization!!! the strange thing is that it seems that every thread has a local copy of the variable i, for example, th1 increments its variable i to 20 than it goes to sleep, after, maybe, th2 wakes and get the lock on the object, shouldn't it increment the value of i to 21???
but it uses the last value it used!!! please, run it, explain me why am I seing this result!!!

Of course each object has a local copy of i. There is nothing magical about a Thread object that would change the expected behavior of any object. What I think you meant to do was put i in the SomeObject class. Then you will see it varying independantly of the thread. Look at this code:


Hope this helps,
Michael Morris
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok!!! u are right!!! sorry for all this!!! I will post meaningful topics!!!
thanks a lot!!! i did not recognized that the problem was in the variable i!!!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi leandro,
Hey dude, chill. I didn't mean anything offensive and there is no such thing as a meaningless topic. If something is giving you a problem it is by definition a meaningful topic. Everyone has a duh moment every once in a while. So please accept my apologies if my undiplomatic post offended you.
Michael Morris
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

leandro oliveira wrote:

ok!!! u are right!!! sorry for all this!!! I will post meaningful topics!!!


Using less punctuation and starting the sentences with a capital letter will also help everybody.
Eugene.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for worring! I did not get offended, i recognized that I need to worry more about other people, and then, post a topic that helps them, if they have the same doubts! about the code, you made me see my error in declaring a variable in each thread, now!!! every thread I create, i pay a lot of attention. Once again, Thanks a lot! Don't worry, you were right about the topic!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!