Questions tagged [atomicinteger]

Java's AtomicInteger class

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicInteger.html

java.util.concurrent.atomic.AtomicInteger was new in Java 1.5

121 questions
0
votes
0 answers

NPE when using ThreadLocal + AtomicLong in Java

I'm initializing using public static ThreadLocal count = new ThreadLocal<>(); How do I use AtomicLong method for count? The below throws a NPE: count.get().addAndGet(records.size());
NoName
  • 1,509
  • 2
  • 20
  • 36
0
votes
1 answer

AtomicInteger vs synchronized block

I have a problem where I need to synchronize access to array of integers in java. My code looks something like this. Class X { int[] counters = new int[100]; Object lock = new Object (); void increment (int idx){ …
grk939
  • 53
  • 1
  • 7
0
votes
1 answer

Removing negative values when variable is an atomic vector

I have a large dataset of a survey (originally a Stata(.dta) file). I would like to use the code below to convert negative values in my dataset to NA. If a variable has more than 99% NA's the code should drop it. #mixed data WVS <-…
Tom
  • 2,173
  • 1
  • 17
  • 44
0
votes
2 answers

Can compareAndSet fail for all threads?

I'm using AtomicInteger to compare and set synchronization state. Here is it private final AtomicInteger state = new AtomicInteger(1); public void tryDo(){ if(state.compareAndSet(1, 2)){ //do some usefule } } The question is if the…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
3 answers

AtomicInteger does not get incremented in JUnit tests

I have the following JUnit tests, and can't work out why the second test does not pass, the value of i is 1 in both tests. public class TestTest { private AtomicInteger ai = new AtomicInteger(1); @Test public void test1() { int i =…
Bentaye
  • 9,403
  • 5
  • 32
  • 45
0
votes
4 answers

Update AtomicInteger from other thread

I have a class that creates many new objects each on their own thread and I would like to keep a running count across the threads. I wanted to have an AtomicInteger but it's not doing what I expected and instead just gets a smaller version. I'm…
LivingRobot
  • 883
  • 2
  • 18
  • 34
0
votes
2 answers

doubts on AtomicInteger and on printing a bi-dimensional array

I have 2 doubts today. 1) I am trying to print a bi-dimensional array (matrix Nx) and I am using this method: System.out.println(Arrays.toString(Matr)); the matrix has only int variables. This is the output, why? [[I@15db9742, [I@6d06d69c,…
DarkPassenger
  • 67
  • 1
  • 8
0
votes
2 answers

How to persist an AtomicInteger in hibernate instead of Integer?

I try to persist an object that have an AtomicInteger variable instead of an Integer using the hibernate java framework (I need use the object in a thread safe scenario after save it) but when i try to save my object java…
Martin
  • 1,282
  • 1
  • 15
  • 43
0
votes
2 answers

AtomicInteger getAndUpdate to zero on Java 6

I am trying to use a counter for detecting number of unique words in a text send over HTTP methods. As I need to ensure concurrency on the value of this counter, I have changed it to AtomicInteger. In certain cases I would like to get the current…
Kit Ostrihon
  • 824
  • 2
  • 14
  • 36
0
votes
1 answer

Object of class containing AtomicInteger's fails to be converted to JSON using GSON

I am trying to serialize an Object of Class (let's say MyClass) Here is roughly how MyClass.java looks like: public class MyClass { private static final AtomicInteger variableOne = new AtomicInteger(); private static final AtomicInteger…
Nick Div
  • 5,338
  • 12
  • 65
  • 127
0
votes
1 answer

Wait for daemon threads to complete an iteration using an executor service

I have to parallelize an existing background task such that instead of serially consuming 'x' resources, it parallely finishes the job at hand using only 'y' threads (y << x). This task constantly runs in the background and keeps processing some…
0
votes
1 answer

get() vs intValue() methods in AtomicInteger

AtomicInteger class has 2 methods, get() and intValue() with following definitions. intValue() definition : /** * Returns the value of this {@code AtomicInteger} as an {@code int}. */ public int intValue() { return get(); } get()…
Sreekanth Karumanaghat
  • 3,383
  • 6
  • 44
  • 72
0
votes
1 answer

Incrementing int counter with visibility

I have a situation where one thread updates int and another one at some point reads it. So single-reader single-writer. So far I was using volatile int for that purpose, but since this forces full sync on memory barriers I was thinking about…
ivenhov
  • 311
  • 3
  • 12
0
votes
1 answer

Behavoir of Threads with AtomicInteger

The following problem is on a mock exam for the OCP Java SE 7 Programmer II Exam. The solution says the answer is 0, but my colleagues and I are not sure that the answer isn't between -5 and 5 (which is a choice) Could someone clarify this for us?…
Shoikana
  • 595
  • 4
  • 8
0
votes
2 answers

AtomicInteger or LongAccumulator

Can someone tell if LongAccumulator could be a better alternative for AtomicInteger in the below example? import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public…
1 2 3
8 9