Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

AtomicLong

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have a homework assignment and I have to look at some code and find a bug and fix the code.

It has an import that I'm not familiar with.



I learned that AtomicLong increments but does it Loop also?

Thanks.


P.S.

Please correct me if I have done anything incorrectly in the asking of this question.
 
Saloon Keeper
Posts: 5882
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AtomicLong is a class, not a looping construct - have you read its javadocs yet? Did they make sense to you? If not, start here: https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html
 
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an AtomicLong in a loop, certainly - but it doesn't do any looping for you.  Similarly, you can use a local variable like "int i = 0"  in a loop, but the variable doesn't do the looping for you.
 
Marshal
Posts: 7272
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Is this the code before the "fix" or after?
 
Liutauras Vilda
Marshal
Posts: 7272
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royal Summers wrote:So, I have a homework assignment and I have to look at some code and find a bug and fix the code.


Royal Summers wrote:


Do you have an idea of what this comment is trying to tell? Does the counter need to resolve to an odd number always?
 
Royal Summers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the Directions:

"Directions
Now that you have learned about how to debug an application in Spring, it's time to put that knowledge to work. In this Hands-On exercise, you will locate the source of the error in the MVC web application provided for you below. The goal of this project will be to fix the problem with the following program. The IDs are not generated as expected. The goal of generating new IDs for each message was to ensure the IDs could only be odd numbers (i.e., 1, 3, 5, 7, etc.). Please zip your project directory and upload when you are finished. This Hands-On will be graded, so be sure you complete all requirements.

Requirements
Bug recognition: The cause of the bug must be identified.
Bug Fix: The bug must be removed from the application, such that the application operates as intended.
Create a new STS project called "lesson-9-project-part-1". Ensure that the package location is com.example.demo. Then, create a new class called GreetingController that contains the following content:"

The code that was provided was the first time the AtomicLong was introduced.  I did research and didn't see AtomicLong having iteration functionality but I thought that I would post my question just to confirm my saneness.


 
Sheriff
Posts: 6381
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I did research and didn't see AtomicLong having iteration functionality


Do you mean increment?  It has that, but I would use addAndGet(long) if you want odd numbers.
 
Royal Summers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, "iteration"  also known as "looping."

": the repetition of a sequence of computer instructions a specified number of times or until a condition is met"

https://www.merriam-webster.com/dictionary/iteration
 
Mike Simmons
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason why you think you need iteration functionality in AtomicLong?  This sounds a bit like asking why the car's steering wheel does not have braking technology.
 
Royal Summers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I asked if AtomicLong 'loops' due to the fact when I Searched for it on the Internet I saw nothing of it looping which appears that a loop would be required in order to determine if the ID's were odd numbers per the Assignment. So, since I understand how a car works, I would never pose that question but since I had no clue how AtomicLong works then I posed the question just for confirmation.

Hope this clarifies?
 
Liutauras Vilda
Marshal
Posts: 7272
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royal Summers wrote:So, I asked if AtomicLong 'loops'...


No, it doesn't loop. Because AtomicLong isn't a loop, but an object, which represents long value, a number in simple words. If you want loop, you need loop (sounds silly, but that's what it is). Numbers can be used in a loop though.

Here are two loops, one contains primitive number, while the other AtomicLong object.

They both print the same:
0
1
2
3


I don't know what else to add here, unless I completely misunderstanding you.
 
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royal Summers wrote:. . . ": the repetition . . . a specified number of times or until a condition is met" . . .

I don't think there is any need for there to be a specified number of times or until a condition is met; iteration simply means repetition and it is possible for it to go on for ever. That is of course the archetype of a program gone wrong.
 
Royal Summers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your input.  I wasn't familiar with this Method and just needed to confirm whether or not I had to add a loop or if this Method did it.

Again, thanks for your time and input!
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!