Results 21 to 25 of 25
- 04-21-2009, 10:06 AM #21
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
- 04-22-2009, 09:55 AM #22
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
hmm... I've never really looked into it. It's something I always recognized, and so I never looked into it deeply. However, it regards the fact that a Thread may access data that is, by the the time the access is complete, incorrect. This is due to fields being edited at the same time by different threads. A happens-before relationship is established when the programmer ensures that a field or method that is accessed by multiple Threads can only be accessed by one at a time. The most common way of doing this is through synchronization.
Fundamentally, a happens-before relationship means that one Thread will always recognize that another Thread edited the field being accessed, whether that access is achieved directly or through methods.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 04-22-2009, 11:36 AM #23
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
Is it means one Thread actions are viewable to other ThreadsFundamentally, a happens-before relationship means that one Thread will always recognize that another Thread edited the field being accessed, whether that access is achieved directly or through methods
- 04-22-2009, 04:27 PM #24
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Think of "happens before" has meaning "will behave as expected if occurs in time before...". In terms of how the Java Virtual Machine is strictly defined to operate, there's a list of certain threading-related actions which are defined to have a "happens before" relationship.
But it's not necessarily the most practical way of remembering how to program correctly. The golden rule you need to keep in your head is that if data is accessed by more than one thread, you need to do something to make sure that that access is safe. That "something" could be using synchronized, volatile, final, using one of the concurrency utility classes such as ConcurrentHashMap etc etc. I'd recommend learning about these things rather than getting too bogged down in the JVM definition to start with.
The thing that generally goes wrong in threading/concurrent programming is when people try to be too clever and deliberately flout the basic rules that they know. For example, they invent some clever way to "avoid synchronization". So long as you just follow the rules rather than trying to come up with some clever way to not follow the rules, you'll generally be fine...Neil Coffey
Javamex - Java tutorials and performance info
- 04-24-2009, 06:10 AM #25
Member
- Join Date
- Feb 2009
- Posts
- 96
- Rep Power
- 0
Similar Threads
-
Java Threads Interview Question
By _tony in forum Threads and SynchronizationReplies: 20Last Post: 12-22-2008, 08:31 AM -
can you give-me a little hand about java threads?
By masdrobeda in forum New To JavaReplies: 3Last Post: 11-10-2008, 05:15 PM -
How to use Java threads
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks