Time |
S |
Nick |
Message |
05:28 |
|
|
nanoz joined ##friendlyjava |
18:16 |
|
mr_lou |
If I have a static String in class A, and this class draws this String on screen in a loop, and class B changes the String - can I risk getting a NullPointer exception if class B updates the String in the exact same moment class A draws it? |
18:20 |
|
aditsu |
mr_lou: first, that could only happen if you have multiple threads |
18:21 |
|
aditsu |
and I think you should only get NPE if B sets the string to null |
18:21 |
|
aditsu |
unless you have some other stuff in there |
18:22 |
|
aditsu |
but anyway, it's better to use some kind of synchronization or "run later" mechanism |
18:38 |
|
mr_lou |
I've never liked synchronization for some reason. |
18:38 |
|
mr_lou |
I mean the synchronized keyword |
18:38 |
|
mr_lou |
But.... I'm not actually using multiple threads... so.. I guess I'm in the clear. |
18:39 |
|
mr_lou |
Thanks aditsu |
18:40 |
|
mr_lou |
If I did use multiple threads though, I would assume that Java puts a lock on objects while changing them.... |
18:40 |
|
aditsu |
java only puts a lock if you tell it to |
18:41 |
|
mr_lou |
...which is done with the synchronized keyword... |
18:41 |
|
aditsu |
that's one way, there are a bunch of other ways |
18:41 |
|
mr_lou |
mkay |
18:41 |
|
aditsu |
a simple one is volatile |
19:07 |
|
mr_lou |
Basically, synchronized just means that only one other object can access it at any given time, right? |
19:09 |
|
aditsu |
in general, you synchronize a block on a specific object; only one thread can do that on the same object at the same time, other threads have to wait to enter that block until the current one finishes |
19:10 |
|
aditsu |
you can synchronize different blocks on the same object, and it still applies |
19:10 |
|
mr_lou |
Right |
19:11 |
|
mr_lou |
But with the simple stuff I do in JavaME, I shouldn't ever need to do that. |
19:11 |
|
aditsu |
I don't know, if you have multiple threads, you might need to |
19:11 |
|
mr_lou |
Best to stick with a single thread for those small homebrew game projects.... though I do of course use extra threads for internet traffic... |
19:13 |
|
* mr_lou |
is working on a new blu-ray game |
19:13 |
|
mr_lou |
"The UFO Game!" |
19:13 |
|
mr_lou |
For PlayStation 4. |
19:13 |
|
* aditsu |
is implementing a generic trie |
19:14 |
|
* mr_lou |
nods pretending he knows what that is |
19:14 |
|
aditsu |
https://en.wikipedia.org/wiki/Trie |
19:15 |
|
mr_lou |
Ah |
21:58 |
|
|
aditsu joined ##friendlyjava |