synchronized

How do you ensure multiple threads can safely access a class field?

When a class field is accessed via a getter method by multiple threads, how do you maintain thread safety? Is the synchronized keyword sufficient? Is this safe: public class SomeClass { private int val; public synchronized int getVal() { return val; } private void setVal(int val) { this.val = val; ...

Java Web Application Sync Question

Let's say I have a class in my web app called class "Foo". It has an initialise() method that is called when the bean is created using Spring. The initialise() method then tries to load an external service and assign it to a field. If the service could not be contacted, the field will be set to null. private Service service; publi...

deadlock on synchronized ( String intern())

I user sun jdk 1.5 ThreadPoolExecutor( 24, 24,60,TimeUnit.SECONDS, new LinkedBlockingQueue()). soemtime I use jdb tool to find the status of all threads in thread pool are " waiting in a monitor", the code is : String key = getKey(dt.getPrefix(), id); synchronized (key.intern()) { -----> Is there a problem in "synchronized (...

In Java critical sections, what should I synchronize on?

In Java, the idiomatic way to declare critical sections in the code is the following: private void doSomething() { // thread-safe code synchronized(this) { // thread-unsafe code } // thread-safe code } Almost all blocks synchronize on this, but is there a particular reason for this? Are there other possibilities? Are ther...

Java synchronized methods: lock on object or class

The Java Tutorials say: "it is not possible for two invocations of synchronized methods on the same object to interleave." What does this mean for a static method? Since a static method has no associated object, will the synchronized keyword lock on the class, instead of the object? ...

Is there a way to stream a stored Flash video to multiple people in sync, but let one of them control playback (play/pause/seek/etc.)

Hi, I don't know much about deploying Flash video streaming solutions, and I want to know if the following can be accomplished in Flash. I am creating an application where multiple people can watch a video at the same time , but one of those people controls the playback of the video for everyone. Furthermore, the controller is not alw...

Is HttpSession thread safe, are set/get Attribute thread safe operations?

Also, does the object that is being set have to be thread safe in order to guarantee that we know what the state of the object stored in session is known. Also, I was reading on the web that some suggest using: synchronized(session) { session.setAttribute("abc", "abc"); } Is this a valid suggestion? ...

Purpose of empty synchronized block in Java?

I was looking through a Findbugs report on my code base and one of the patterns that was triggered was for an empty synchronzied block (i.e. synchronized (var) {}). The documentation says: Empty synchronized blocks are far more subtle and hard to use correctly than most people recognize, and empty synchronized blocks are almost...

What is a good naming convention for a routine that sets a global variable in the same class

Code Complete (Chapter 7, Section 3) says that a good function should be named for the value it returns and a good procedure name in a class should be named for what it does. When I write synchronized methods in Delphi (pre 2009) I sometimes need to use them to set global variables, a seemingly bad programming practice, but a necessar...

proper usage of synchronized singleton?

So I am thinking about building a hobby project, one off kind of thing, just to brush up on my programming/design. It's basically a multi threaded web spider, updating the same data structure object->int. So it is definitely overkill to use a database for this, and the only thing I could think of is a thread-safe singleton used to con...

Synchronised Comboboxes

How do I get 2 synchronized comboboxes so that changing the index of the one automatically changes the other one. ...

Is using synchronized on a Java DAO going to cause issues?

Is using the 'synchronized' keyword on methods in a Java DAO going to cause issues when used by a web application? I ask because I have a multi-threaded stand alone application that needs the methods to by synchronized to avoid resource conflict, as seen here. java.util.concurrent.ExecutionException: javax.persistence.PersistenceExcept...

Java Synchronized and Threads

I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also: Shared Object: public class ThreadValue { private String caller; private String value; public ThreadValue( String caller, String value ) { this.value = value; this.caller = caller; } public sync...

java synchronized issue

I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also: Shared Object: public class ThreadValue { private String caller; private String value; public ThreadValue( String caller, String value ) { this.value = value; this.caller = caller; } public synchronized String getValue() { ...

Java Thread Shared Object Synchronization Issue

I'm having issues with Synchronized not behaving the way i expect, i tried using volatile keyword also: Shared Object: public class ThreadValue { private String caller; private String value; public ThreadValue( String caller, String value ) { this.value = value; this.caller = caller; } public synchronized String getValue() { ...

@synchronized in a static method

In Objective-C, you can declare a block as being synchronized on some object by using the @synchronized construct. It would look something like this: @synchronized (self) { // Do something useful } However, I'm curious what exactly self is referring to when you have a static method (+ instead of -). I tried looking through the App...

using synchronized in Threads

What could be the understanding of the following? I have gone through this post at SO but still at a loss to assemble it. code1: synchronized(this){ // some code } code2: Object lock = new Object(); synchronized(lock){ // some code } Any tutorial, or some links to explain synchronized like they explain it to a child? ...

Synchronization in threads for Java

I have a home grown web server in my app. This web server spawns a new thread for every request that comes into the socket to be accepted. I want the web server to wait until a specific point is hit in the thread it just created. I have been through many posts on this site and examples on the web, but cant get the web server to proceed...

Synchronize on BlockedQueue.

Hello, I have a code piece that I am reviewing (using FindBug). public class MyClass{ ... private BlockedQueue q = new LinkedBlockingQueue<MyData>(1000); private static final batchSize = 1000; public boolean testMethod(){ boolean done = false; synchronized(q){ if(q.size == batchSize){ q.notify(); done...

How can I synchronized two process accessing on the same resources?

I have two processes which access to the same physical memory(GPIO data addr). So how can I have synchronized between these apps? I understand that we have some kind of locking mechanism such as mutex and semaphore, so which method is the fastest? Thank for your help, -nm ...