• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Learning abt threads..

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone.. please explain...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops..sorry..i didn't include the full code.Here is the full program
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic