Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Learning abt threads..  RSS feed

 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just learning about threads.
I came across a program..
The character c doesn't increment after the first thread execution.So except thr first thread,all the threads print B(before and after modification).
If I declare the variable char c as local variable as in 1 c value gets modified and prints A,B,C.
I know that each thread has its own copy of local variables.

why the variable is not incremented if it is a instance variable?
Why the variable gets modified perfectly if it is a local variable?

please explain....
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone.. please explain...
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to see more code... specifically, how are the thread objects created? how many? how are they started? etc.

Since the program prints a result, it may help to send the output of the two cases too.

Henry
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops..sorry..i didn't include the full code.Here is the full program
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have at least two major problems.

1. Your "for" loop is probably not what you intended. The closing brace seems to be in the wrong place. And if you meant it to be there, then I don't have a clue what you expected the output to be.

2. While you do synchronize, it is still a race condition as each thread has its own copy of the c variable -- which is set purely on ordering.

In any case, the program acts differently mainly by the race condition, (and somewhat because your loop is probably wrong) and probably not because of your changes from an instance variable to a local variable.

Henry
[ April 08, 2005: Message edited by: Henry Wong ]
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for ur reply.The loop is just to printout the StringBuffer contents three times before modification .The loop is written to meet my assignment requirements.
1.how come the loop affect the result(as u said to some extent)?
2.I have found a way to avoid race condition
here it goes..

Is this the only way?Is there any other way to avoid race condition in my previous code without making the variable c as local one?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!