Java Synchronized Object?

The synchronized keyword synchronizes on the whole object instance not just the setter. I would rather go for a fine grained locking strategy or better... use a thread safe data structure where you store and get the received data. I personally love the BlockingQueue.

The synchronized keyword synchronizes on the whole object instance not just the setter. I would rather go for a fine grained locking strategy or better... use a thread safe data structure where you store and get the received data. I personally love the BlockingQueue where T is the type of data you receive on the network.So suppose you are receiving Objects over a socket: public class ReceivedDataHolder{ BlockingQueue dataBuffer = new LinkedBlockingQueue(); //... public void dataReceived(Object data){ dataBuffer.

Offer(data); } public Object getReceivedData(){ return dataBuffer.take(); } } And in your socket you could do this whenever you receive data: receivedDataHolder. DataReceived(object); Any thread that wants to get data should do: receivedDataHolder.getReceivedData(); This latter method call will block the calling thread until there is an element available on the queue (check this out for more details) I hope this helps.

2 BlockingQueue is an interface, so new BlockingQueue(); won't compile. You must use a concrete class implementing the interface, such as ArrayBlockingQueue or LinkedBlockingQueue, which are the most commonly used concrete blocking queues for producer/consumer. – Bruno Reis Dec 22 '11 at 4:12 @BrunoReis Indeed, a little mistake of mine.It is now fixed – GETah Dec 22 '11 at 9:02.

Maybe AtomicReference would be suitable for you. See: java.util.concurrent. Atomic Java volatile reference vs. AtomicReference.

All objects in java has something called Intrinsic locks, If any thread wants to do any operation on any object then it needs to acquire the intrinsic lock of that object. It will guarantee that only 1 thread will process your block of code at any given time. A thread can acquire lock on any object, if that object is not locked by any other thread, if it is locked then the thread will wait till the other thread releases the lock on that object.

If you use synchronized block, your code will be somewhat like this public void SomeObject exampleGetter() { synchronized(this) { if(this. IsReceiving) return oldCachedObject; else return currentObject; } In this case when your thread enters the synchronized block, if any other thread is having lock on this object, then it will wait till that thread releases the lock. And if that object is free then your thread will acquire the lock on this object and perform the operation and then release the lock on that object.

For further information on synchronized blocks, methods and intrinsic locks, refer docs.oracle.com/javase/tutorial/essentia... I hope it helped you :).

2 Totally non-sense: "If any thread wants to do any operation on any object then it needs to acquire the intrinsic lock of that object". Any thread can do anything with any object it knows about without acquiring any kind of lock. – Bruno Reis Dec 22 '11 at 4:14.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions