| | |
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,597
Reputation:
Solved Threads: 202
0
#2 26 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: 972
Reputation:
Solved Threads: 145
0
#3 26 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; 26 Days Ago at 6:16 am. Reason: Added useful tutorial link
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 4
0
#4 26 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; 26 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 |
account android api applet application array arrays automation bidirectional binary birt bluetooth chat class classes client code columns component data database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner screen search server set size sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree windows






