วันพฤหัสบดี, ตุลาคม 12, 2549

Java: ว่าด้วยเรื่อง volatile และ final

== volatile ==
มาเริ่มกันที่ volatile ก่อน. เวลาที่ VM มันรัน ในแต่ละ thread มันจะมี local copy ของ variables ของ thread นั้นๆ เพื่อประสิทธิภาพในการทำงาน (หลัก locality)
ทีนี้ถ้าเรามี field ที่มี type เป็น primitive ง่ายๆ อย่างเช่น int ซึ่งมี thread 2 อัน ใช้งานฟิลด์นี้แล้วล่ะก็
ค่าที่แต่ละ thread นั้นเอาไปใช้อาจจะไม่ได้ sync กันก็ได้

volatile เป็น keyword ที่เราอาจจะคิดได้ว่าเป็นการบอก optimizer ว่าไม่ต้องทำ local copy ของ variable ตัวนี้นะ ให้ใช้จาก main memory มาทุกๆครั้ง มันก็จะไม่มีปัญหาเรื่อง data inconsistency แม้ว่าค่านี้จะถูก ใช้งานโดยหลายๆ thread ก็ตาม

ข้อแตกต่างจาก synchronized ที่เห็นชัดคือ synchronized นั้นบอกว่าให้ถือล็อคให้ได้ก่อนแล้วค่อยเข้าไปใน monitor นะ แล้วพอออกจาก monitor มันจะ sync ค่า variable ใน local copy ทั้งหมดกลับไปด้วย

ref: http://www.javaperformancetuning.com/news/qotm030.shtml

==final==

สำหรับ final สับสนกว่า เพราะมันใช้ modify ได้ตั้งแต่ variables, fields, methods, ไปยัน class เลย.
การใช้ final เพื่อเข้ามาจำกัดเมคคานิซึ่มของภาษาบางอย่างเพื่อประโยชน์ต่างๆ เช่น robustness, correctness, performance.

การใช้ final modifier ใน Java constructs แต่ละอย่างสรุปได้ดังนี้:

  1. variables: a final variable เซตค่าได้แค่ครั้งเดียวเท่านั้น.
  2. fields: a final field เซตค่าได้ครั้งเดียวเช่นกัน, อาจจะโดย constructor ของคลาสนั้น หรือโดย static block.
  3. methods: a final method ทำให้ไม่สามารถ override (nor hidden ได้ถ้า กรณีที่ไม่ใช่ private) Note that a private method also implies final.
  4. classes: a final class ห้าม extend.
ref:
http://renaud.waldura.com/doc/java/final-keyword.shtml
http://mindprod.com/jgloss/final.html

วันอังคาร, ตุลาคม 10, 2549

Desktop PC administration note : upgrade BIOS of mainboard and RAID controller

IntelDesktopBoard D865GBF BIOS version P24 -> P25
Silicon Image 0680 PCI-RAID controller 3.2.10 -> 3.2.20

วันอาทิตย์, ตุลาคม 01, 2549

An attempt to install TimeSys Linux 2.6

I tried to compile and install TimeSys Linux 2.6, a real-time OS based on Linux kernel from TimeSys corp. The kernel installation was succeeded, BUT there're few problems I cannot solve. At the boot time, there are many device probe failure said about ``modprobe QM_MODULES function not implemented'' and my wireless PC-Card was not functioned---said the device is busy or in use. Here are some quick and dirty note on the installation.

  1. cd to the $KERNEL directory
  2. There is no .config file at first place, so I have to make it by ``make menuconfig'' and save to get the default? config.
  3. Configure the kernel by make xconfig is much easier to see the options, so I choose to do so. Unfortunately, it required QT which I didn't have. Therefore, I was forced to install QT first from the installation CD #2 and #3. Actually, other alternative is to use ``make gconfig'' which run on Gtk, instead of ``make xconfig''.
  4. Configure the kernel. For the power management issue, I think ACPI is preferable ove APM. Make sure that EXT3 File System is selected otherwise, reboot will fail.
  5. make
  6. make modules_install
  7. make install
  8. About device probing failure, I searched the web and found that module-init-tools should be install after installing the new kernel. Get it from kernel.org and install as usual: ./configure --prefix=/ && make && make install
  9. then generate modprobe.conf by execute $ generate_modprobe.conf /etc/modprobe.conf