j2me networking, threads and deadlocks ตัวอย่างโคดสั้นๆ ของการเอารูปจากเน็ตมาแสดง

|
http://stackoverflow.com/questions/867660/j2me-networking-threads-and-deadlocks

KISS: Keep it simple, stupid!

|

วิธีใช้ SAX Parser

|
1. ก่อนอื่นก็ต้อง import package ที่จำเป็น
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

2. ต้องมี class ที่ extends DefaultHandler
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
class ParserHandler extends DefaultHandler {
...
}

3. เริ่มใช้งาน
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
ParserHandler parser = new ParserHandler();
String str = saxParser.parse( InputStream, parser );

4. ใน class ParserHandler ต้องมี method พวกนี้อยู่
สมมุติเรามี <title>....</title> มันจะถูกเรียกเมื่อเจอ <title>, qName คือ title ส่วน attributes คือลิสต์ของ attribute ใน <title>
public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
......
}
ถูกเรียกเมื่อเจอตัวอักษรอื่นๆ ระหว่าง tag
public void characters (char buf [], int offset, int len) throws SAXException
{
......
}

ถูกเรียกเมื่อเจอ </title> มักจะใช้ push อะไรออกมา
public void endElement (String uri, String localName, String qName) {
......
}


คีย์เวิร์ด volatile ใน Java

|
volatile เป็นคีย์เวิร์ดที่ใช้เวลาเขียนโปรแกรม Java ที่เกี่ยวข้องกับ thread เอาไว้ใช้ควบคุมเวลาเราต้องการติดต่อกันระหว่าง thread เพื่อให้มั่นใจว่าข้อมูลจะถูกต้องตามที่เราต้องการ

เนื่องจากการทำงานแบบ thread แต่ละ thread จะมี memory ของตัวเองซึ่งอาจจะไม่สามารถเห็นระหว่างกันได้ และเราไม่สามารถกำหนดลำดับการทำงานได้ เราไม่มั่นใจได้ว่า thread แรกจะทำงานไปถึงขั้นตอนไหนแล้ว ขณะที่ thread ที่สองถึงขั้นตอนนี้

The Java volatile modifier is an example of a special mechanism to guarantee that communication happens between threads. When one thread writes to a volatile variable, and another thread sees that write, the first thread is telling the second about all of the contents of memory up until it performed the write to that volatile variable.


จากรูปถ้าเรากำหนด ready เป็น volatile เมื่อ thread2 มาอ่านตัวแปร ready, thread2 จะอ่าน memory ที่ thread1 มีก่อนที่จะสั่ง ready = true มาด้วย จึงมั่นใจได้ว่า thread2 จะ print 42

แต่ถ้าไม่เป็น volatile ตอนที่ thread2 มาอ่านตัวแปร ready แล้วได้ true ไป แต่ตัวแปร answer ไม่ได้ไปด้วยก็จะ print ออกมาผิด

tip! ถ้า ready เป็น volatile นอกจาก thread2 จะ update memory ของ thread1 ก่อนสั่ง ready = true แล้ว ยังจะ update memory ของทุกๆ thread ที่มีการ set ค่าให้ ready อีกด้วย

ที่มา - http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html

การลง Application บน BlackBerry นอก App World

|
1. อัพโหลดไฟล์ alx ขึ้นไปบนเว็บแล้วให้ BlackBerry เข้าทาง URL นั้น
2. ใช้ Desktop Manager

ตัวอย่าง navigationMovement และ tab

|
navigationMovement
http://supportforums.blackberry.com/t5/Java-Development/Tab-Navigation/m-p/381156

tab
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800508/How_To_-_Create_tabbed_view_screens.html?nodeid=1357482&vernum=0

ทำเลียนแบบ App World ต้องดูให้ได้

|
important
http://www.blackberry.com/DevMediaLibrary/view.do?name=HowToExtendManager
normal
http://www.blackberry.com/DevMediaLibrary/view.do?name=HowtoExtendtheScreen