Sunday, August 14, 2011

some java basics

It's another hot, hot day in Tokyo.
Days like this make me realize how important it is to live in a place that has good air circulation (i.e. wind flow 風当たり). Of course, that's often difficult when buildings are built close together, meaning that convenient places tend to have less of a breeze.

Anyway, I came across some good articles clarifying some Java fundamentals. The basics are not necessarily simple.

Java is Pass-by-Value!
Specifically, Java passes object references by value.
* The above article includes a reference to an article explaining C++ Reference Variables, a good explanation of the general concept of reference variables.

Exploring Java: Threads

* What is a daemon thread? When should I use setDaemon() and why?
Use thread.setDaemon(true) to tell the JVM to make the thread a daemon thread.

According to Webster's, a daemon (variant of demon) is an attendant power or spirit. Daemon threads are typically used to perform services for your application/applet (such as loading the "fiddley bits"). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution. Use daemons as the minions they are.

[In short: daemon threads do not keep the program from quitting; user threads keep the program from quitting. -Alex]

Synchronized block vs. method:
The only real difference is that a synchronized block can choose which object it synchronizes on. A synchronized method can only use 'this' (or the corresponding Class instance for a synchronized class method).
- http://stackoverflow.com/questions/574240/synchronized-block-vs-synchronized-method/574253#574253
* http://stackoverflow.com/questions/416183/in-java-critical-sections-what-should-i-synchronize-on
* http://stackoverflow.com/questions/417285/equivalent-code-for-instance-method-synchronization-in-java
* http://www.coderanch.com/t/234017/threads/java/Threads

No comments: