2015年4月18日 星期六

[IT] 解決 Chrome瀏覽器的異常,像是玉山 webatm不能用,旺旺 百度等 plugin

發生原因:Chrome 不在預設支援 NPAPI


Chrome 的 NPAPI 支援

Web 瀏覽器的 Java 外掛程式有賴於所有主要 Web 瀏覽器長久以來一直支援的跨平台外掛程式架構NPAPI。Google 於 2013 年 9 月宣布計畫將在 2014 年底終止 Chrome 的 NPAPI 支援,進而實際上停止對 Silverlight、Java、Facebook 影片以及其他類似 NPAPI 形式之外掛程式的支援。但最近 Google 修改了其計畫,現在宣佈他們計畫在 2015 年下旬完全移除 NPAPI。在 2015 年 4 月,Google 從 Chrome 版本 42 開始,新增了額外的步驟,以設定執行以 NPAPI 為基礎的外掛程式 

解決方法 :

在 Chrome 版本 42 及更新版本啟用 NPAPI

在 Chrome 版本 42 中,需要額外的組態步驟以繼續使用 NPAPI 外掛程式。
  1. 在您的 URL 列輸入:
    chrome://flags/#enable-npapi 
  2. 按一下啟用 NPAPI 組態選項的啟用連結
  3. 按一下組態頁面底端出現的立即重新啟動按鈕

2015年4月13日 星期一

[JAVA] java memory mode with "volatile" & "synchronized"

As a matter of fact, the Java memory model is such that a modification
to a variable in one thread may not immediately be visible to other threads. Actually, it
may never be visible. Consider the code in Listing 5–11: if one thread calls
MyClass.loop(), and at some point in the future another thread calls
Myclass.setValue(100), the first thread may still not terminate; may carry on looping
forever and always print out a value other than 100, simply because of the Java
language’s memory model.
Listing 5–11. Java Memory Model Impact
public class MyClass {
     private static final String TAG = "MyClass";
     private static int mValue = 0;
     public static void setValue(int n) {
          mValue = n;
     }
     public static void loop () {
          while (mValue != 100) {
               try {
                    Log.i(TAG, “Value is ” + mValue);
                    Thread.sleep(1000);
               } catch (Exception e) {
                    // ignored
               }
          }
     }
}
You have two options to fix that:
Use the synchronized keyword, as shown in Listing 5–12.
Use the volatile keyword, as shown in Listing 5–13.
Listing 5–12. Adding the Synchronized Keyword
public class MyClass {
     private static final String TAG = "MyClass";
     private static int mValue = 0;
     public static synchronized void setValue(int n) {
          mValue = n;
     }
     public static synchronized int getValue() {
          return mValue;
     }
     public static void loop () {
          int value;
          while ((value = getValue()) != 100) {
          try {
               Log.i(TAG, “Value is ” + value);
               Thread.sleep(1000);
               } catch (Exception e) {
              // ignored
               }
          }
     }
}

Listing 5–13. Adding the Volatile Keyword
public class MyClass {
     private static final String TAG = "MyClass";
     private static volatile int mValue = 0; // we add the volatile keyword and remove
     the synchronize keyword
     public static void setValue(int n) {
          mValue = n; // you’d still have to use synchronized if that statement were mValue += n (not atomic)
     }
     public static void loop () {
          while (mValue != 100) {
               try {
               Log.i(TAG, “Value is ” + mValue);
               Thread.sleep(1000);
               } catch (Exception e) {
              // ignored
               }
          }
     }
}

2015年4月12日 星期日

[Android] Handlers and Loopers

Android defines two classes in the android.os package that will often be the
cornerstones of the interthread communication in your multithreaded applications:
Handler
Looper
While creating an AsyncTask object hides the Handler and Looper details from you, in
some cases you need to use handlers and loopers explicitly, for example when you need
to post a Runnable to a thread other than the main thread.

you use a
Handler object to post a Runnable in a Looper’s message queue. Your application’s main
thread already has a message queue, so you don’t have to create one explicitly.
However, the threads you create do not come automatically with a message queue and
message loop, so you would have to create one yourself if needed. Listing 5-8  shows
how you can create a thread with a Looper.
Listing 5–8
public class MyThread extends Thread {
     private static final String TAG = “MyThread”;
     private Handler mHandler;
     public MyThread(String name) {
          super(name);
     }
     public Handler getHandler() {
          return mHandler;
     }
     @Override
     public void run() {
          Looper.prepare(); // binds a looper to this thread
          mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
               switch (msg.what) {
               // process messages here
               }
          }
     };
     // the handler is bound to this thread’s looper
     Looper.loop(); // don’t forget to call loop() to start the message loop
      // loop() won’t return until the loop is stopped (e.g., when Looper.quit() is called)
     }
}
Android provides an easier way to work with looper threads with the HandlerThread
class, which also makes it easier to avoid the potential race condition mentioned in
Listing 5–8, where getHandler() may still return null even after the thread has been
started. Listing 5–9 shows how to use the HandlerThread class.
Listing 5–9. Using the HandlerThread Class
     public class MyHandlerThread extends HandlerThread {
     private static final String TAG = "MyHandlerThread";
     private Handler mHandler;
     public MyHandlerThread(String name) {
          super(name);
     }
     public Handler getHandler() {
          return mHandler;
     }
@Override
     public void start() {
          super.start();
          Looper looper = getLooper(); // will block until thread’s Looper object is initialized
          mHandler = new Handler(looper) {
               @Override
                    public void handleMessage(Message msg) {
                         switch (msg.what) {
                         // process messages here
                    }
               }
          };
     }

}

2015年4月7日 星期二

[Android][Java] Java define 4 reference type

Strong
Soft
Weak
Phantom

it is important to avoid memory leak
Android’s LruCache uses strong references