Threads and Data Structures

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 5
Reputation: eleal is an unknown quantity at this point 
Solved Threads: 0
eleal eleal is offline Offline
Newbie Poster

Threads and Data Structures

 
0
  #1
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
25 Days Ago
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark
 
0
  #3
25 Days Ago
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; 25 Days Ago at 6:16 am. Reason: Added useful tutorial link
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 22
Reputation: Devoted Hosting is an unknown quantity at this point 
Solved Threads: 4
Devoted Hosting Devoted Hosting is offline Offline
Newbie Poster
 
0
  #4
25 Days Ago
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; 25 Days Ago at 6:31 am.
Devoted Hosting
High Quality Shared And Reseller Hosting
cPanel, 24/7 support, 99.9% uptime guaranteed
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC