I need an array to put Car objects Car is a class that extends JPanel
This array will be accessed severe times by some class�s So searching is involved The size of the array in unknown. So must be growable. And finally 10 entirely separate threads will access this array. This 10 threads will be started like t1.start(); t2.start(); ��.. ��.. t10.start(); t1 to t10 is thread objects So we don�t know what thread will access the array first or what time or���. So synchronizes in needed
My problem is without implementing an array to suet this features what collection I can use
Thank you
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
On which criteria will your searches be based?
As an aside, having a Car class inherit from JPanel sounds like a very questionable desing to me.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sameera Abeysinghe
Ranch Hand
Joined: Nov 15, 2004
Posts: 104
posted
0
One by one One after another Will not be like giving a index and access in the middle Will only be accessed one after another
Jan Groth
Ranch Hand
Joined: Feb 03, 2004
Posts: 456
posted
0
sounds like a perfect situation to use a Vector, which is a synchronized, growable arraylist.
you might want to check the java-doc, or have a look at the collection part of sun's java tutorial.
many greetings, jan
Sameera Abeysinghe
Ranch Hand
Joined: Nov 15, 2004
Posts: 104
posted
0
Ok so I create a vector object with initialCapacity= 10 and capacityIncrement = 1 I use vector.add() to add elements How can I access the elemnt? By vector.get() Will it remove the element from that vector???
And finals when I use any methods in the vector since it is synchronize I don�t have to bother about putting notify of notifyall or synchronize or what ever
Is it???
How about if I use vector.toArray() in each of my 10 threads and the do this operation using that object array Is it ok ???
Originally posted by Sameera Abeysinghe: Ok so I create a vector object with initialCapacity= 10 and capacityIncrement = 1 I use vector.add() to add elements How can I access the elemnt? By vector.get() Will it remove the element from that vector???
No. You must use remove(int i) for that. get(int i) only returns the element without removing.
And finals when I use any methods in the vector since it is synchronize I don’t have to bother about putting notify of notifyall or synchronize or what ever
Is it???
Well if only the access to the List (yes with capital L - you should declare it as a List so you can switch implementations easily) then you don't need any other synchronization. If you want to remove items but wait when the List is empty until some other Thread fills it then you need additional wait / notify / notifyAll statements.
How about if I use vector.toArray() in each of my 10 threads and the do this operation using that object array Is it ok ???
Only if none of the Threads need to modify the List. Otherwise the array will be a snapshot, and not get updated.
I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options: 1) Vector 2) call Collections.synchronizedList(List l) with an ArrayList
I don't see any advantages to using the second option - you create another wrapper object around your existing List, and you still get synchronization - something that is done using Vector immediately. [ November 01, 2005: Message edited by: Rob Spoor ]
I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options: 1) Vector 2) call Collections.synchronizedList(List l) with an ArrayList
I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options: 1) Vector 2) call Collections.synchronizedList(List l) with an ArrayList
Keep in mind that using a synchronized collection doesn't really guarantee thread safety. Often you will need to synchronize a sequence of actions, when one call on the collection relies on the result of a prior call.