Results 1 to 8 of 8
Thread: Some questions
- 05-19-2012, 03:22 PM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Some questions
Hi!
I'm reading the "Killer Java Game Programming" book, and I was wondering why are these variables set to static?
My other question is, what does the volatile keyword do, and why would you use it?Java Code:private static final int PWIDTH = 500; // size of panel private static final int PHEIGHT = 400;
-
Re: Some questions
They are being declared as constants, and constants should be final and static -- final because they shouldn't be allowed to change, and static because the class only should have one instance of the constant.
Have you had a chance to look this up first? It's usually best to do a Google search, and then ask a question about what exactly you don't understand from your reading.My other question is, what does the volatile keyword do, and why would you use it?
-
Re: Some questions
- 05-19-2012, 03:33 PM #4
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Some questions
Well I saw a tutorial where he declared a variable volatile like this:
He said that the reason he used volatile was to avoid a huge crash later in the application?Java Code:private volatile boolean running = false;
-
Re: Some questions
- 05-19-2012, 03:50 PM #6
Re: Some questions
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-19-2012, 05:55 PM #7
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Some questions
I don't understand this:
What is main memory, and synchronized blocks?What is the Java volatile keyword?
Essentially, volatile is used to indicate that a variable's value will be modified by different threads.
Declaring a volatile Java variable means:
The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.
We say "acts as though" in the second point, because to the programmer at least (and probably in most JVM implementations) there is no actual lock object involved. Here is how synchronized and volatile compare:
-
Re: Some questions
Synchronized blocks of code are code blocks that have limits on other code's ability to call such that only one thread can call the block at a time, to prevent threads from clashing.
Threads have a local memory and can hold copies of variables that they are using (the cached thread-local variables). If the thread makes changes to this cached variable, it is not reflected in the original variable held in main memory (not in the thread's local memory), and so other objects that may be using that variable might not see changes in the variable caused by the first thread.
Suggestion: read some tutorials on java thread as this can be pretty deep stuff. You'll use volatile on variables that are used by many threads and you want all threads to see changes to the variable as soon as they occur.
Similar Threads
-
Help for some questions
By king4oneday in forum EclipseReplies: 1Last Post: 02-23-2012, 04:59 PM -
3 questions
By silverglade in forum New To JavaReplies: 11Last Post: 05-09-2011, 03:17 PM -
Some Questions
By MuslimCoder in forum New To JavaReplies: 2Last Post: 02-25-2009, 04:01 PM -
I have Questions -_-
By ChazZeromus in forum New To JavaReplies: 5Last Post: 09-13-2008, 08:08 PM -
Questions About JSP?
By mtz1406 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-19-2008, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks