FAQ Database Discussion Community


Java Multithreading - What Really Happens When Accessing A “Locked” Object?

java,multithreading,locking,synchronized
Given the following object defined and initialized in a wrapper class: // (thread-safe) List of Requests made by users private static List<Request> requests = Collections.synchronizedList(new ArrayList<Request>()); The following code is being called constantly in an update loop: // <-- (Thread 1 executes the following code @ Runtime=1.0000000ms) synchronized(requests) { for...

Thread synchronizing, using notifyAll()

java,multithreading,synchronized
This is an exercise from Bruce Eckel's Thinking in JAVA 3rd Edition, Revision 4.0, from chapter 13 (exercise 10). I am supposed to have a class that acts like a chef which gives out orders and notifies the waiters, and two waiters who wait for an order to be ready,...

How did this synchronized code break?

java,android,android-asynctask,synchronized
I have the following snippet of code. synchronized (mObject) { if (mObject.variable != -1) { doThis(); doThisAsWell(); andThis(); insertObjectInDb(); // This is crashing because mObject.variable is -1 } } To my knowledge, if I use synchronized on an object, and then run that block, the value of the variable can't...

Thread-Safe Singleton Synchronized() in Objective-C

objective-c,thread-safety,singleton,synchronized
In our app, we use singletons in several locations, and recently I went through and added @synchronized commands to all of the singleton methods to ensure that they are thread-safe. My question is what the difference is between calling this: + (RLReceiver *) getReceiver { static RLReceiver *receiverCache; @synchronized(receiverCache) {...

How to use .join() with Thread at this particular Thread

java,android,multithreading,synchronized
My application is making some heavy Algorithm calculation at a very high speed (not high as USB data input - will be explained shortly). Each time a serial data enters the phone's USB port it activates the Algorithm if it's not already running. The algorithm is Threaded because I want...

swiprefreshlayout keep refreshing inside onscroll method

android,android-layout,android-listview,synchronized,swiperefreshlayout
Im having a SwipeRefreshLayout and there's a listview inside it. I want to implement pull up to refresh(since swipe refresh only support pull down refresh), hence did a bit modification on the onscroll method of the list view @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, final int totalItemCount)...

can we put thread join method inside synchronized method

java,multithreading,synchronized
Can I use thread.join inside a synchronized method? class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ System.out.println(n*i); try{ Thread.join(); }catch(Exception e){System.out.println(e);} } } } can I use thread.join instead of wait?...

Synchronized property reading as non-synchronized property

java,multithreading,synchronized
I have a non-atomic java property, which can be set by synchronized setter. My question is, can I read this property by non-synchronized getter? Thanks.

synchronized method in for loop

java,multithreading,synchronization,synchronized
In the following simple example code (mainly taken from this nice udemy video) there are two threads that increment a count instance variable through a synchronized method. This method however is inside a for loop in each thread. My question is: In a case like that, is it possible that...

Multiple locks - Behind the scene

java,multithreading,synchronized
class A { Object lock1 = new Object(); Object lock2 = new Object(); List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); void insert1() { synchronized (lock1) { list1.add(5); } } void insert2() { synchronized (lock2) { list2.add(5); } } void firing() { for (int i = 0; i...

Changing the same variable for all existing subthreads

java,multithreading,resize,synchronized
sorry in advance if that question did get asked before somewhere here. I really tried to find something that helps me. Sadly I had no luck. Here is my situation: I have a Frame class with a button and a slider. And I have a thread class. With the button...

Synchronized static methods behaviour under inheritance in java

java,multithreading,static,synchronized
I read somewhere: If the static synchronized methods are located in different classes, then one thread can execute inside the static synchronized methods of each class. One thread per class regardless of which static synchronized method it calls. Suppose I have below class hierarchy: public class Base { public static...

Name of the thread not being shown properly in transaction system

java,multithreading,synchronized
I have created a transaction system which seem to work perfectly with synchronized threading. However, I want the name on the person doing the transaction, in the following case (Emily and John) to be displayed instead of thread number. Following is the code: public class SharedAccountDriver { static int balance...

difference between locks and synchronized methods [duplicate]

java,methods,concurrency,synchronized,locks
This question already has an answer here: Synchronization vs Lock 6 answers I understand that synchronization allows for implicit locks, but don't these produce the same results? What is difference between the following two sections of code? Why would a programmer choose to use each? Code block #1 class...

Block/Re-queue other sidekiq jobs from processing when existing sidekiq job is processing a particular resource

ruby-on-rails,postgresql,ruby-on-rails-4,synchronized,sidekiq
I have sidekiq jobs doing processing on a many types of resources. However, for a particular type of resource, eg: Resource X, I need to ensure that only 1 sidekiq job can process that particular resource at any given time. For example, if i have 3 sidekiq jobs that gets...

Running wait() on a Thread instance from within main() in Java

java,multithreading,wait,synchronized
I am playing around with the timed version of wait() in java.lang.Object and have observed that it acts differently in two different scenarios. Scenario1: Using the default definition of run() in Thread public static void main (String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); synchronized(t) {...

Unable to understand Class object

java,multithreading,class,synchronized
Oracle Java documentation on Intrinsic Locks and Synchronization says: You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class....

Nested synchronized block

java,multithreading,concurrency,synchronized
Let's imagine I have next classes: public class Service { public void transferMoney(Account fromAcct, Account toAcct, int amount) { synchronized (fromAcct) { synchronized (toAccount) { // could we use here only one synchronized block? fromAcct.credit(amount); toAccount.debit(amount); } } } } class Account { private int amount = 0; public void...

Program gets halted: wait() and notify()

java,multithreading,wait,synchronized,notify
I am trying to achieve this: Created two different threads, one prints odd numbers, one prints even numbers. Once one thread prints a number, it has to wait for the other thread and so on, that is one-after-other. To achieve this, i am using synchronized block along with wait() and...

Synchronized methods to avoid deadlock

java,multithreading,deadlock,synchronized,locks
I have two synchronized methods and I'm using the mediator design pattern. I'm trying to avoid deadlocks, which is (from what I understand) for example when a thread has a lock on a variable res1 but needs a lock on variable res2. The other thread needs the lock for res1...

Understanding Xcode crash message and @synchronized directive

ios,xcode,multithreading,atomic,synchronized
Trying to write some code that uses iCloudKit singleton in addition to a global variables singleton to share variables and processes across multiple method calls, potentially from multiple iPads. And I have two questions in one. Code crashes with this message right now. 2015-06-09 18:31:18.879 iBeacons Demo[277:24157] *** Terminating app...