aspose file tools*
The moose likes Threads and Synchronization and the fly likes Trying To Cause Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Trying To Cause Problems" Watch "Trying To Cause Problems" New topic
Author

Trying To Cause Problems

sean cotter
Greenhorn

Joined: Sep 16, 2009
Posts: 4
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?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
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?


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
sean cotter
Greenhorn

Joined: Sep 16, 2009
Posts: 4
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

Joined: Jan 13, 2009
Posts: 357
use static variables, so the same instance is shared between all classes ;-)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sean cotter
Greenhorn

Joined: Sep 16, 2009
Posts: 4
Thank you Henry!! I am now able to visualize how this works in my head.

I changed the main() to:



Thanks again!
 
 
subject: Trying To Cause Problems