顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2010年12月27日 星期一

Java在Linux中文顯示問題

於java安裝目錄(i.e., ubuntu 10.10為/usr/lib/jvm/java-6-sun/)下的jre/lib/fonts新增fallback目錄
$ mkdir fallback
接下來就是把想要的字型,看是用複製的或是symbolic link到fallback目錄下

$ ln -s /usr/share/fonts/truetype/ttf-droid/DroidSansFallback.ttf .
搞定!!!

2010年12月21日 星期二

Install sun jdk on ubuntu 10.10


Android requires JDK5.0 to build the source code (at the time of writing).
(When build Android, the java and javac version is checked by */build/core/main.mk)
So, you need to: download and install the JDK5.0 from Sun's (Oracle's) website, or, use the old repositories:

1a. For Java 5, Add Jaunty to repositories
$ sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu/ jaunty multiverse"

1b. For Java 6, Add Jaunty to repositories
$ sudo add-apt-repository ppa:sun-java-community-team/sun-java6
2. Update source list  
$ sudo apt-get update
3. Install 
$ sudo apt-get install sun-java6-jdk (or sun-java5-jdk)

2009年6月18日 星期四

bad class file. class file has wrong version 50.0, should be 4x.0.

When compiling your code using the JAR files, you may receive an error similar to the one below:
packages/apps/DMClient/src/com/asus/dm/DMMMIFactory.java:3: cannot access com.redbend.vdm.MmiChoiceList

bad class file: out/target/common/obj/JAVA_LIBRARIES/XXXX_intermediates/javalib.jar(com/XXXX/vdm/MmiChoiceList.class)

class file has wrong version 50.0, should be 49.0

Please remove or make sure it appears in the correct subdirectory of the classpath.

import com.XXXX.vdm.MmiChoiceList;
^

1 error

These types of errors are caused by a Java version mismatch between the compiled proxies and your runtime.

Below are a list of some of the version numbers and their corresponding Java runtimes:


Version 50.0 = Java 1.6.x
Version 49.0 = Java 1.5.x
Version 48.0 = Java 1.4.x


To resolve this issue, compile them with the right version of Java SDK

2009年4月21日 星期二

搭配interrupt()終止thread

Thread.interrupt()的迷思

切記勿使用Thread.stop()。雖然它確實停止了一個正在執行的thread,然而,這種方法是不安全也是
Deprecated.的,這意味在未來的JAVA版本中,它將不復存在。

至於
Thread.interrupt(),並不會中斷一個正在執行的thread,如以下程式碼:

class Example1 extends Thread {

    public static void main( String args[] ) throws Exception {
Example1 thread = new Example1();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Interrupting thread..." );
thread.interrupt();
Thread.sleep( 3000 );
System.out.println("Stopping application..." );
}

public void run() {
while(!stop){
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();

while((System.currentTimeMillis()-time <>
}
}

System.out.println("Thread exiting under request..." );
}
}
其輸出結果為:

Starting thread...
Thread is running...
Thread is running...
Thread is running...
Interrupting thread...
Thread is running...
Thread is running...
Thread is running...
Stopping application...
Thread is running...
Thread is running...
Thread is running...

甚至,在call Thread.interrupt()後,Thread仍會繼續執行。


真正地中斷一個Thread


中斷thread最好的方式是,使用shared variable,以通知thread必須停止正在執行的任務。thread必須週期性的核查這一variable(尤其在冗餘操作期間),然後有秩序地中止任務,如以下所示:

class Example2 extends Thread {
volatile boolean terminate = false;

public static void main( String args[] ) throws Exception {
Example2 thread = new Example2();
System.out.println( "Starting thread..." );
thread.start();
Thread.sleep( 3000 );
System.out.println( "Asking thread to stop..." );
thread.terminate = true;
//thread.interrupt();
Thread.sleep( 3000 );
System.out.println( "Stopping application..." );
}

public void run() {
while ( !terminate ) {
System.out.println( "Thread is running..." );
long time = System.currentTimeMillis();

while ( (System.currentTimeMillis()-time <>
}
}

System.out.println( "Thread exiting under request..." );
}
}

此方式基本上可行,然,當tread因某些原因(如,Object.wait()、hread.sleep()、ServerSocket.accept()和DatagramSocket.receive()等)被block或處於waiting queue中,該thread便不能check shared variable(i.e. 上面程式碼中的terminate變數),自然就無法停止。所以,要使用某種機制使得線程更早地退出被阻塞的狀態。

很不幸運,不存在這樣一種機制對所有的情況都適用,但是,根據情況不同卻可以使用特定的技術。


使用Thread.interrupt()中斷thread


如以上所述,Thread.interrupt()並不會中斷一個thread。實際上,此方法作用是,在thread block時拋出一個中斷signal,這樣thread就得以退出block的狀態。更確切的說,如果thread被Object.wait, Thread.join和Thread.sleep三 種方法之一阻塞,那麼,它將接收到一個中斷異常(InterruptedException),從而提早地終結被block狀態。

因此,如果thread被上述幾種方法block,正確停止thread方式是設置shared variable,並搭配interrupt(),i.e., 將第2段程式碼中的comment (i.e., //thread.interrupt() )解開即可。如果thread沒有被block,這時使用interrupt()將不起任何作。在任何一種情況中,最後thread都將check shared variable然後再停止。

以下為javadoc原文:

interrupt

public void interrupt()
Interrupts this thread.

First the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.


Throws:
SecurityException - if the current thread cannot modify this thread

2009年4月14日 星期二

LinkedBlockingQueue< E >

Producer跟comsumer這種pattern在程式設計很實用也常見到,在此pattern通常會搭配一個FIFO queue,意即,producer負責把東西塞進queue裏,而consumer再從中取出。

之前用到此pattern的開發經驗是,使用database時,會以一獨立之thread扮演consumer角色負責將queue中的sql command取出並依序執行之。藉此,在程式執行期間,程式與database間只會有一connection至於避免與database間建立太多conncetion的優點是...
是...
是...
我真的忘記了...囧。

總之,建立太多connection會有一些問題產生,因此才有人開發了connection pool這一套件,用以限制與database的connection數量..(這樣應該凹的不會太過分吧..XD)。

之前開發的時候為了應用此pattern,當初我們還藉由ArrayList、vector、hashtable之類的class自己implement一class。但,今天看了google的souce code後發現,若非有特殊需求,其實,只要用java.util.concurrent裡的LinkedBlockingQueueArrayBlockingQueue即可。

以下為j2se java doc的說明內容:

public class LinkedBlockingQueue
extends AbstractQueue
implements BlockingQueue, Serializable

An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

This class is a member of the Java Collections Framework.



google用到的function:

boolean offer(E o)
Inserts the specified element at the tail of this queue if possible, returning immediately if this queue is full.
Etake()
Retrieves and removes the head of this queue, waiting if no elements are present on this queue.

Java keyword -- transient

物件序列化這個在C++時代就有被討論的議題,Java於1.1以後提供一個介面
Serializable實作此序列化機制。所謂序列化,就是能將記憶體(Memory)中的
實體物件(Object Instance)以位元流(byte stream)方式儲存於永久媒體如
硬碟,此過程叫序列化。而且之後也能從硬碟中再讀取該物件於記憶體中重
建,並回復先前狀態。

Java實作物件序列化非常簡單,你只需將想要序列化的物件去實作(implements)
java.io.Serializable介面,其他序列化的工作,JVM會幫你完成,你會發現,
Serializable介面中沒有定義任何方法(Method),其實它是一個Marker Interface
它只是告訴JVM此物件可以被序列化!!

- transient
此為Java保留字,告訴JVM以transient宣告的基本型態(primitive type)或物件
(object)變數不要序列化,例如敏感性資料像是密碼等,或是在其他環境不
可獲得的資源如JDBC及Network Socket等。總之只要基本型態或物件以
transient宣告,JVM就不會將它序列化。

2009年4月8日 星期三

Interface data member sumary

  1. interface會自動將其member (包括data,function)設為public,會自動將其data member 宣告成static final.

  2. interface可宣告data member但必須是static final 且initailized.

Example: int i = 5;
/*合法,因為如上述, java預設interface中的data member為static final modifier*/