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
1
vote
3 answers

Is it possible to mutate an int through a method using AtomicInteger?

I have a method: public static void MutableInt (int newValue) { AtomicInteger atomicInteger; atomicInteger = new AtomicInteger(10); newValue = atomicInteger.get(); } And then invocation in main: int x=3; MutableInt(x); …
ForInfinity
  • 166
  • 1
  • 12
1
vote
4 answers

Printing numbers in sequence using 3 threads

I have a program where 3 Threads are trying to print numbers in sequence from 1 to 10. I am using a CountDownLatch to keep keep a count. But the program stops just after printing 1. Note: I am aware that using AtomicInteger instead of Integer can…
Anurag
  • 723
  • 3
  • 13
  • 31
1
vote
0 answers

Having trouble using locks correctly

I thought I had a fairly decent understanding of locks and basic multi-threading concepts, but I'm royally messing something up here. All the program is supposed to do is receive a filename for a text file and the number of threads you want to use…
Stravask
  • 11
  • 1
  • 3
0
votes
2 answers

Java Thread Atomic Integer checking not working?

im newbie with multi-threads and im trying to make a program that use it. Technically, it has a class with executor and a worker class. The worker class takes the info to work on, process it, if there are more infos to processes the worker calls the…
Myphre
  • 11
  • 2
0
votes
1 answer

multi level filtering by index of customized list in java 11

Instead of adding and updating these extra sequential number fields, I found useful two stage solution when processing well sorted multi-level collections for GUI (where row indexes are calculated on fly): ParentEntity…
0
votes
3 answers

AtomicInteger & lambda expressions in single-threaded app

I need to modify a local variable inside a lambda expression in a JButton's ActionListener and since I'm not able to modify it directly, I came across the AtomicInteger type. I implemented it and it works just fine but I'm not sure if this is a good…
akmsw
  • 113
  • 4
0
votes
1 answer

Compare and swap value in atomic integer kotlin

Hey I am learning atomic integer in kotlin. I want to know is there atomic integer swap value if current value is smaller than new value. For example AtomicInt 10 Scenario 1 new value is 5 and it changes to 5 because 5 is lower than 10 Scenario 2…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
0
votes
2 answers

Atomic Variables Accessed within inner classes

So I am doing a sort here and implementing the Comparator interface using an inner class, exceptionMessage and didJsonParsingFailed are variables declared outside the inner class, now java doesn't allow accessing of local variables using inner…
0
votes
2 answers

Java 8 variable should be final or effectively final issue

I am using Java 8 stream Iteration with a variable that should be used in other classes also. So I have used the below code. AtomicBoolean bool = new AtomicBoolean(true); public void testBool(){ list.stream().forEach(c->{ if( c.getName() !=…
Neela
  • 107
  • 2
  • 10
0
votes
1 answer

How to do comparison and adding AtomicInteger

There is such synchronized function: private int balance; //... public synchronized void executeRequest(Request request) { switch (request.getType()) { case CREDIT: if(balance >= request.getAmount()) { …
0
votes
0 answers

Is there a way to access the date added to an ArrayList in a different to change its data?

I have this in the Class: public static ArrayList studentInfo = new ArrayList();I created a method CreateStudent() that basically allows the user to create a student using Scanner, the DisplayStudent() uses ObjectInputStream to open…
0
votes
1 answer

Java auto increment from base

In my case I have class and subclass. Problem is when I search my function increment id automatically. When I first time click search it upload 5 ids, when I add one and go back to search it upload 5 ids I have + (5 old + 1 new). Then if I want to…
None4
  • 13
  • 4
0
votes
1 answer

How to observe AtomicInteger variable value change in android?

I am using AtomicInteger in my android app. Now I want to listen to AtomicInteger variable value changes, how can I listen? private final AtomicInteger hostsCount = new AtomicInteger(0); hostsCount.set(10); hostsCount.set(101); How can I…
Chaitanya Karmarkar
  • 1,425
  • 2
  • 7
  • 18
0
votes
1 answer

When AtomicInteger is faster than synchronized

I've already read a great number of articles where is said that AtomicInteger class works faster than a synchronize construction. I did some tests on AtomicInteger and "synchronized" and in my tests, it occurs that synchronized is much faster than…
0
votes
2 answers

Atomic Integers values are not getting updated in threads

I have defined an atomicinterger variable in class. The class also extends thread class .I have created two threads and incrementing the value of the atomic integer in run method .I was excepting the value of 2 after running two threads, But I am…
Karthikeyan
  • 111
  • 11
1 2 3
8 9