Its tricky to get this right.
AraryBlockingQueeu does exactly what you want.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ArrayBlockingQueue.html
thanks for the responses. I am not sure that i want anythig in
particular, just looking for advice from the experienced...so not sure
ArrayBlockingQueue is what i was looking for. Basically, i would like
to know if there are performance issues with one thread accessing a
property of a class in another thread, if that class has a while loop
that is processing most of the time...for instance, below is the
scenario. In this case, will ThreadC have an issue accessing the
Vector? Now i realize that synchronzing may be an issue, but not
really asking about that...what i want to know is that when ThreadA
wants to access MyClass.list, does it have to wait for processing in
MyClass.process to finish doing 'somehting', or can it access it
immediately since it has a reference to it already?
class MyClass{
public Vector list;
public MyClass( ){ }
public process( )
{
while ( true ) { \\do something that processes 90% of the time}
}
}
class ThreadClass implements Runnable {
private Vector pointerToMyClassVector;
public ThreadClass( Vector list ) {
pointerToMyClassVector = list;
}
public void run( ){
....
list.add( xxx );
....
}
}
....
void main ( String[] args )
{
MyClass myClass = new MyClass( );
ThreadClass threadC = new ThreadClass( myClass.list );
( new Thread ( threadC ) ).start( )
myClass.process( );
}
Wibble - 10 May 2005 04:47 GMT
If your not synchronizing you can access it immediately.
That being said, you need to make sure its state isnt changing
while your accessing it. Thats where synchronization comes in.
Usually you'll wrap critical sections within synchronize where
an object may be modified. Synchronization isnt free,but it isnt
that expensive either. Its like likely to have more of an affect
on latency than throughput. An instance doesnt really live in a thread.
It lives on the heap. The only thing really thread specific is
the current owner of a synchronization monitor.
> thanks for the responses. I am not sure that i want anythig in
> particular, just looking for advice from the experienced...so not sure
[quoted text clipped - 43 lines]
> myClass.process( );
> }
farseer - 10 May 2005 07:13 GMT
thanks much for the response.