| | |
Threads and Data Structures
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
-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.
•
•
Join Date: Sep 2008
Posts: 1,610
Reputation:
Solved Threads: 205
0
#2 30 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.
•
•
Join Date: Apr 2008
Posts: 977
Reputation:
Solved Threads: 145
0
#3 29 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
http://java.sun.com/docs/books/tutor.../locksync.html
Last edited by JamesCherrill; 29 Days Ago at 6:16 am. Reason: Added useful tutorial link
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 4
0
#4 29 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; 29 Days Ago at 6:31 am.
Devoted Hosting
High Quality Shared And Reseller Hosting
cPanel, 24/7 support, 99.9% uptime guaranteed
High Quality Shared And Reseller Hosting
cPanel, 24/7 support, 99.9% uptime guaranteed
![]() |
Similar Threads
- Data Structures (C++)
- data structures in c++ (C++)
- Data Structures??? (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the Java Forum
- Previous Thread: online exam project on java
- Next Thread: Threads in an instant messaging program
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






