944,144 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 349
  • Java RSS
Nov 7th, 2009
0

Threads and Data Structures

Expand Post »
I've got the following question about threads in Java: Isn't there a problem if:
-There's a main program which creates a series of threads. Each thread needs to have access (and to modify) to the same object of a user defined class, and this object has references to a hashtable and a Vector (those are fields of this object)
?

If there's a problem, I'd like some hints to solve it.

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eleal is offline Offline
5 posts
since Nov 2009
Nov 8th, 2009
0
Re: Threads and Data Structures
Yes, there is a problem, because these threads could potentially access/modify the data at 'unfortunate' times. For example, thread 1 modifies variable A to 2, then thread 2 immediately modifies A to 3 (as a simple but stupid example). Another possibility being that threads 1 and 2 are both using variable A in a series of operations. So both threads want A to maintain some state while they do those operations, but the other threads modifies A, which interferes.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 8th, 2009
0
Re: Threads and Data Structures
Yes, it is a standard problem. The solutions is called a "lock". Each thread has to get a lock on the object before it modifies it, then releases the lock when its finished. If a thread has already locked the object the second thread must wait until the lock is released. This guarantees that only one tread can modify the object at any one time. Have a little Google for Java threads locks - there's lots of tutorial stuff at every level of detail.

http://java.sun.com/docs/books/tutor.../locksync.html
Last edited by JamesCherrill; Nov 8th, 2009 at 6:16 am. Reason: Added useful tutorial link
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Nov 8th, 2009
0
Re: Threads and Data Structures
Granted I'm fairly new to Java, but could you not declare variables as synchronized to help solve this problem?
Last edited by Devoted Hosting; Nov 8th, 2009 at 6:31 am.
Reputation Points: 12
Solved Threads: 6
Light Poster
Devoted Hosting is offline Offline
29 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: online exam project on java
Next Thread in Java Forum Timeline: Threads in an instant messaging program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC