impleDateFormat為非線程(Thread)安全, 多線程時會造成問題。

方法一:
直接用static
缺點:不同線程的安全性(皆能共享)

方法二:
使用時才new一個
缺點:消耗內存, 影響效能

方法三:
最推薦的方式為透過ThreadLocal

ThreadLocal可根據不同線程, 存取不同的值,
舉個例子:

final int num = 10;
final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    threadLocal.set(num);
    Log.e("adam", "test, 1, get: " + threadLocal.get());
    new Thread(new Runnable() {
    @Override
    public void run() {
        threadLocal.set(20);
        Log.e("adam", "test, 2, get: " + threadLocal.get());
    }
}).start();
    new Thread(new Runnable() {
    @Override
    public void run() {
        Log.e("adam", "test, 3, get: " + threadLocal.get());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.e("adam", "test, 4, get: " + threadLocal.get());
            }
        });
    }
}).start();

結果:
test, 1, get: 10
test, 2, get: 20
test, 3, get: null
test, 4, get: 10

 

Thread中沒設值時, 取出的是null, UIThreadhread設的是10, 並沒有因為第一個Thread設值20而改變, test 4依然是10。

由此可確認,ThreadLocal的存取是獨立於不同線程。
SimpleDateFormat透過ThreadLocal:

private static final ThreadLocal<DateFormat> dateFormatThreadLocal = new ThreadLocal<DateFormat>();
private static DateFormat getDateFormat(String format){
    DateFormat dateFormat = dateFormatThreadLocal.get();
    if(dateFormat == null){
        dateFormat = new SimpleDateFormat(format);
        dateFormatThreadLocal.set(dateFormat);
    }
    return dateFormat;
}

Android String&StringBuilder&StringBuffer比速-附代碼及數據


說明:
String 為常量,每次必創造新常量賦值,耗內存及效能
StringBuilder及StringBuffer為變量, 後者可synchronized線程安全

實驗:
分別跑三萬筆累加字串,測試效能。

結果:
2020-01-06 15:58:32.670 4166-4166/adamtest, String: 24794(ms)
2020-01-06 15:58:32.685 4166-4166/adamtest, StringBuilder:5(ms)
2020-01-06 15:58:32.705 4166-4166/adamtest, StringBuffer:18(ms)

結論:
單線程盡量用StringBuilder, 多線程保障安全時才用StringBuffer

int count = 30000;
long startNs = System.nanoTime();
String str = "";
for(int i=0;i&lt;count;i++){
    str = str + i;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
LogUtils.e("adam", "adamtest, String: "+ tookMs+"(ms) "+str);

long startNs2 = System.nanoTime();
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i&lt;count;i++){
    stringBuilder.append(i);
}
long tookMs2 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs2);
LogUtils.e("adam", "adamtest, StringBuilder:"+ tookMs2+"(ms) "+stringBuilder);

long startNs3= System.nanoTime();
StringBuffer stringBuffer = new StringBuffer();
for(int i=0;i&lt;count;i++){
    stringBuffer.append(i);
}
long tookMs3 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs3);
LogUtils.e("adam", "adamtest, StringBuffer:"+ tookMs3+"(ms) "+stringBuffer);</pre>

TeamViewer for Windows,Mac,Linux,行動裝置 下載


TeamViewer

透過網際網路自由進行遠端存取與遠端桌面共用

簡介:

覺得每次開會要大老遠跑來累了嗎?覺得電腦帶來帶去累了嗎?有TeamViewer在此通通搞定,TeamViewer支援電腦、手機、pad,甚至也有網頁板,TeamViewer提供簡單的方式,只要輸入帳號密碼或輸入IP及密碼進入朋友的電腦,便能遠端到帳號裡的某個裝置,除該遠端者外,裝置前的使用者也能使用,從此遠端不再是件難事,開會不在是問題,由於TeamViewer本來就是提供團隊開會用,TeamViewer的傳輸有加密,如此好用的軟體已在筆者的裝置上都有它一席之地,對於懶的記IP的筆者而言,能用帳號密碼登入簡直是個福音阿!更何況如此好用的東西竟然是免費的!!!(筆者最喜歡這種好用又免費的東西了)當然,根據不同的使用需求,付費版享有更強大更多人同時連線的功能。

筆者的使用情況如上,只要登入後,右方顯示你的裝置,點擊便能遠端。

官方網站:

TeamViewer

程式下載:

TeamViewer完整版 – Windows

TeamViewer完整版 – Mac

TeamViewer完整版 – Linux

TeamViewer應用程式 – 行動裝置

Dropbox 1.4.17 for Windows,Mac,Linux 免費下載


Dropbox

Take Dropbox with you, even on the go

Grab Dropbox apps for iPhone, iPad, Android, and BlackBerry

簡介:

現在雲端的時代,越來越多東西已經放到網路上,傳統的隨身碟受攜帶上及容量的限制,此時Dropbox出現了!不論是桌電、筆電、pad、手機,都有Dropbox(網頁也能上下載)可以使用,讓你資料不用再帶來帶去,只要放到Dropbox的空間,處處都能更改並且自動同步(不同地方的資料一致),新註冊使用者馬上享有2GB免費空間,完成新手任務或邀請朋友更能增加容量,最多可增加16GB,現在又出現更優惠的活動,校園爭奪戰!只要用學校信箱認證,根據學校的分數(參加人數及完成任務)最多可增加25GB,說這麼多還不趕快行動!筆者已經增加了15GB往25GB邁進~Happy!這東西真是太好用了~

另外,有個人付費使用(100、200、500GB每年99、199、499美金)及企業團隊使用(1TB給5人使用,有14天試用期),只要線上信用卡付費即可。

校園競賽活動:

Great Space Race! – Dropbox

官方網站:

Dropbox – Simplify your life

程式下載:

Dropbox 1.4.17 for Windows

Dropbox 1.4.17 for Mac

Dropbox 1.4.17 for Linux

SecurityKISS Tunnel網路代理伺服軟體(跳板軟體)


SecurityKISS Tunnel

網路代理伺服軟體

 

自從認識了許多大陸朋友後得知,對岸竟然無法用facebook、youtube、甚至連此網站wordpress也不能,因此找到了這款SecurityKISS Tunnel,一款非常簡單使用的跳板軟體,雖然其有限制一天容量300MB的大小,但對於一般瀏覽網頁的使用者而言相當足夠了,何況簡單使用對一般人而言才是最重要的。

 

SecurityKISS Tunnel
官方網站:http://www.securitykiss.com/
軟體版本:0.1.5
軟體語系:英文
支援系統:Windows XP/Vista/7
軟體下載:官方下載頁面

 

安裝完畢後,可於兩個藍色主機圖示,選擇跳板的位置

點擊左下角的按鍵(原為connect),連接成功後如下

哈哈 我成功從美國進入囉~

看看本身的IP,果然不同吧!

利用之前介紹的what is my ip查看

沒錯,已經被當作美國連上去的了

Free Alarm Clock可播放音樂的鬧鐘軟體-繁體中文免安裝版


Free Alarm Clock繁體中文免安裝版

連結下列網址下載 Free Alarm Clock繁體中文免安裝版

http://freealarmclocksoftware.com/FreeAlarmClockPortable.zip

使用如下

設定中有貪睡功能  設定幾分鐘後再響一次,也有改變語言的選項

點擊鬧鐘(上午9:00Birds)兩下

可更改時間及撥放音樂(亦可放網路音樂連結)

設定完後鬧鐘可於休眠狀態喚起(若用網路音樂連結則須注意此時網路是否有開啟)