• 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

Trying To Cause Problems

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am playing around with threads trying to get a better understanding. In this case I am trying cause a variable to be corrupted. Unfortunately, is seems I am not sharing it but I do not understand why aren't I.




my output is:
booThread0 is starting!!
The value of I is 0
My thread name is booThread0
**** the value of i is: 0 - Name: booThread0
booThread1 is starting!!
The value of I is 0
My thread name is booThread1
**** the value of i is: 0 - Name: booThread1
booThread2 is starting!!
The value of I is 0
My thread name is booThread2
**** the value of i is: 0 - Name: booThread2
booThread3 is starting!!
The value of I is 0
My thread name is booThread3
**** the value of i is: 0 - Name: booThread3
**** the value of i is: 1 - Name: booThread2
**** the value of i is: 1 - Name: booThread3
**** the value of i is: 2 - Name: booThread2
**** the value of i is: 2 - Name: booThread3
**** the value of i is: 1 - Name: booThread0

Any ideas?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What variable is supposed to be shared???

and there is only one thread running (except the main thread) so how could a racing condition happen?
 
sean cotter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lines 7-9 create 4 threads with names booThread0 thru booThread3, these names can be seen in the output.
The variable being shared is num located on 17 and modified on lines 17, 20 and 34.
Every time a new thread starts the value of num is 0 for that specific thread, as stated by the ouput lines "The value of I is 0", and the fact the the last 4 lines of output say the values for num are 1,2,2,1. Once in the loop I am always incrementing so it should never go from 2 to 1.

How would you modify it so other threads are sharing the same version on num?

Thank you for your help.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use static variables, so the same instance is shared between all classes ;-)
 
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
If you look at line 8, you will see that all four threads have there own copy of a TT1 instance. You will also notice that num is an instance variable, so all the four threads have there own copy of the num variable.

Two possible options to cause problems... (1) give all four threads the same TT1 instance, or (2) make the num variable a static variable.

Henry
 
sean cotter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry!! I am now able to visualize how this works in my head.

I changed the main() to:



Thanks again!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic