| | |
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,595
Reputation:
Solved Threads: 202
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.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
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
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
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 4
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
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 |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






