在Java中 , 我們如果想要使用Thread ,
可以藉由anonymous inner class的方式 ,
for example :
new Thread() {
public void run() {
int j = Nas_Rp.INSTANCE.execute_rsync_cfgs(configs2);
logger.error("thread after execute_rsync_cfgs: j="+j);
}
}.start();
其實這個意思就是:
class MyThread implement Thread
{
public void run()
{
int j = Nas_Rp.INSTANCE.execute_rsync_cfgs(configs2);
logger.error("thread after execute_rsync_cfgs: j="+j);
}
}
MyThread t = new MyThread();
t.start();
另外要注意的一點昰 , 在anonymous inner class外 ,
若想傳參數進去 , 讓該變數在inner class內可用的話 ,
該變數必須要宣告為final , 這個意思主要是不希望該變數被該Thread所更改 ,
應該是為了避免race condition的問題.
(final type跟C++中的const用法相當類似!)
全站熱搜
留言列表