synchronized

Synchonization in Java

Hi, Quick question. I am pretty new to thread-safe programming, and was wondering if I have something like below, would this be safe from deadlock once compiled and run? public class Foo { protected CustomClass[] _mySynchedData = new CustomClass[10]; public void processData() { synchronized(_mySynchedData) { ...

Synchronized and local copies of variables

I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization on local variable 'myMap' Is this the appropriate synchronization and why? Map<String...

Why are synchronize expensive in Java?

I am really new to Java and I read that "synchronized" is "very expensive" in Java. All I want to know is what is expensive and how is it expensive? Thanks. ...

What is the most efficient implementation of a java like object monitor in C++?

In Java each object has a synchronisation monitor. So i guess the implementation is pretty condensed in term of memory usage and hopefully fast as well. When porting this to C++ what whould be the best implementation for it. I think that there must be something better then "pthread_mutex_init" or is the object overhead in java really so...

Volatile or synchronized for primitive type?

In java, assignment is atomic if the size of the variable is less that or equal to 32 bits but is not if more than 32 bits. What(volatile/synchronized) would be more efficient to use in case of double or long assignment. like, volatile double x = y; synchronized is not applicable with primitive argument. How do i use synchronized i...

Synchronizing on two or more objects (Java)

I have code similar to following: public class Cache{ private final Object lock = new Object(); private HashMap<Integer, TreeMap<Long, Integer>> cache = new HashMap<Integer, TreeMap<Long, Integer>>(); private AtomicLong FREESPACE = new AtomicLong(102400); private void putInCache(TreeMap<Long, Integer> tempMap, int fileNr){ i...

C#: Do I have to make ArrayList synchronized if multiple threads only read it

I'm using static ArrayList in a class to store information about non-updatable database fields. I'm planing to initialize it in constructor once (init method call guarded by lock in constructor). After that multiple threads check if arraylist Contains a field. Do I have to control this read access in any way? For example by calling Array...

Why does my thread stop when I try to access a synchronized list?

For some reason, the output of this: public void msgNeedParts() { // Blabla... System.out.println(name + ": Try to print 'tasks'..."); synchronized(tasks) { System.out.println(name + ": Tasks--" + tasks); System.out.println(name + ": Did I manage to print it?"); tasks.add(new BinToDump(feeder, binNum...

java synchronized block for more than 1 objects?

Hi, I have two arrays, and I need to synchronize access to them across threads. I am going to put them in a synchronized block. The problem is, I can pass only one of them to 'synchronized' st one go. How do I ensure that the access to both the arrays is synchronized? Do I put them in a class and create an object of it? Or I access th...

Understanding synchronized

Given this code: public class Messager implements Runnable { public static void main(String[] args) { new Thread(new Messager("Wallace")).start(); new Thread(new Messager("Gromit")).start(); } private String name; public Messager(String name) { this.name...

Java: synchronized(Object) and RejectedExecutionException

Hello I have this problem: I have a few threads which access one object with synchronized(Object) { ... } But sometimes this exception is raised: execute: java.util.concurrent.RejectedExecutionException Why? And what should I do with it? Thanks ...

synchronized method or use spring @transactional?

I have a method that will be used to send out email. i want to lock this method so only one thread can accses it per time and the rest pool up concurrently. should i synchronized the method or use spring @transactional PROPAGATION_REQUIRED ? in my service layer //each time use new thread to send out email public void sendThroughSMT...

synchronized(Object) { } problems

I have ran into a perfomance problem where 880 threads are doing synchronized() { method() } in the same time and this has lead to a major perfomance problem. Is it possible that there is some limit of threads waiting at synchronized()? Where can I get the limit? Another question is what is best to put into synchronized( ? ). Because ...

Side effects of throwing an exception inside a synchronized clause?

Are there any unclear side effects to throwing an exception from within a synchronized clause? What happens to the lock? private void doSomething() throws Exception {...} synchronized (lock) { doSomething(); } ...

Threaded State Machines in Java

Is there a way of Holding a thread in a State waiting for changes? I mean wait tll something happend (change var, call method, etc..) perhaps it needs using Event Listeners, or synchronized objects/methods.. The usual aproach for statemachines like this statemachine example That uses a do{..}while(true) loop that could work for singl...

Java Synchronized Block for .class

What does this java code means? Will it gain locks on all objects of 'MyClass' synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code different from this one: synchronized(this) { //is all objects of MyClass are thread-safe now ?? } ...

Final variable and synchronized block in java

What is final variable ? (if I write final int temp ; in function what is the meaning ?) For which goal use final variable(both class variable and in function variable) ? why variable in synchronized block must declare final ? ...

Can spring transactions unsynchronize a synchronized method?

My colleague and I have a web application that uses Spring 3.0.0 and JPA (hibernate 3.5.0-Beta2) on Tomcat inside MyEclipse. One of the data structures is a tree. Just for fun, we tried stress-testing the "insert node" operation with JMeter, and found a concurrency problem. Hibernate reports finding two entities with the same private key...

Synchronization help in Java

Hello, looking at http://download.eclipse.org/jetty/stable-7/xref/com/acme/ChatServlet.html, I don't seem to understand why there needs to be a synchronization block in a synchronized method, like so: private synchronized void chat(HttpServletRequest request,HttpServletResponse response,String username,String message) throws IOException...

Do I have to synchronize access to encapsulated thread-safe data structures in Java?

Say I have something like this (and I do) class QueBean extends JPanel { private Queue queue = new LinkedBlockingQueue(); public Object poll(){ return queue.poll(); } } with some of these that run on their own threads class ConsumerBean extends JPanel implements Runnable{ private QueBean queBean; public ...