Re: [問題] 兩個thread要reference到同一個object
※ 引述《yangpika (狗狗洋ˇ洋)》之銘言:
: 因為想知道其他thread的值...有人跟我說reference到同一個object就好了
: 就thread把其他所需要的值寫進去 其他thread再去讀就好
: 但是我不知道要怎麼宣告 它要怎樣宣告
: 才能讓兩個threadreference到同一個object??
: 在run的時候分別宣告不就是兩個object嗎?? 還是相同名字就可以了呢??
/**
* Foo has a member field.
*/
public class Foo implements Runnable {
/**
* Other object
*/
private Object obj = null;
/**
* @return obj
*/
public Object getObj() {
return obj;
}
/**
* @param obj other object
*/
public void setObj(Object obj) {
this.obj = obj;
}
/**
* Run your work.
*/
public void run() {
// TODO insert your code here...
}
}
這很簡單。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.117.172.148
推
02/20 21:56, , 1F
02/20 21:56, 1F
→
02/20 23:13, , 2F
02/20 23:13, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 4 篇):