Results 1 to 5 of 5
Thread: Threading Problem
- 02-12-2013, 10:20 PM #1
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
Threading Problem
I have a program that has some thread's and now have problems
One thread (the main thread) keeps adding strings to a TreeMap. (at a quite high rate)
(with map.put(string1,string2);
Another thread (started by pressing a button in a Gui) wants to copy that Tree map to an array.
(with array = map.values().toArray(new String[0]);)
and this never seems to happen, I guess because the main thread keeps adding strings.
map should stay a treeMap because i want to have the array sorted. (and as far as I understand hashmaps don't do that)
what is an easy and safe way to temporarely interrupt the main thread ?
- 02-12-2013, 10:57 PM #2
Re: Threading Problem
Look at using synchronized methods to do the add and the copy.
http://docs.oracle.com/javase/tutori.../syncmeth.htmlIf you don't understand my response, don't ignore it, ask a question.
- 02-13-2013, 12:08 AM #3
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
Re: Threading Problem
tried that
synchronised the add and the getArray methods in the class that contains the map but it doesn't seem to work :(
I even tried synchronising the map with
private final Map<String, String> map = Collections.synchronizedSortedMap(new TreeMap<String,String>());
but it still It just won't work
Curiuosly i use the routine that makes an array from the map also in the main thread and in that thread it works fine.
-
Re: Threading Problem
It's hard to know what's not working without seeing your code, preferably a small compilable runnable program that shows your problem for us, an SSCCE.
- 02-13-2013, 10:24 PM #5
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
Re: Threading Problem
I t looks like i was not really paying attention to my program, did some refactoring and now it works. (think the problem was a testroutine that stopped the thread and because other threads kept running i didn't notice it
made the methods synchronized (and converted them from class to instance methods) and now all seems to work fine :)
Thanks for your help :)
Similar Threads
-
Problem threading?
By vcardell in forum New To JavaReplies: 1Last Post: 05-01-2012, 08:00 AM -
Threading problem
By Andyj in forum Threads and SynchronizationReplies: 3Last Post: 12-05-2010, 09:27 PM -
Problem in Multi threading.
By Chetans in forum Advanced JavaReplies: 3Last Post: 03-23-2010, 04:42 PM -
Getting problem in threading in JAVA
By Chetans in forum Threads and SynchronizationReplies: 3Last Post: 03-19-2010, 07:49 AM -
Problem in threading
By saurabh in forum Threads and SynchronizationReplies: 6Last Post: 12-01-2008, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks