IT界討論區(40) 放咗工吹咩IT呀, 我想打機呀

1001 回覆
6 Like 0 Dislike
2018-08-30 23:09:45
都係singleton
2018-08-30 23:12:47
但你有好多舊同樣嘅class嘅instance喎
singleton普遍係指一個application入面只有一個instance
但你上面個case只係表達到一個application入面唔同job必定會create唔同instance

所以我先問你你一個webapp入面點會做到一個instance?
2018-08-30 23:13:49
一野答哂大家
因為我一路以來做緊web app helper class都只會return static readonly value
基本上唔會有concurrent update情況
所以就咁用static class係咪就唔會有問題?

另外say我個static helper有個好簡單method
就咁static int test(int i){return input i++};

如果我就咁用static class唔用singleton

我有兩個web request同時call呢個function
req1 = test(1);
req2 = tets(5);

咁樣static會唔會炒粉?出現return錯value情況?

利申: 真係乜春都唔識
2018-08-30 23:14:33
2018-08-30 23:17:28
public sealed class Singleton
{
    private static List<Singleton> list = new List<Singleton>();

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            if (list.Count <= 2)
            {
                instance = new Singleton();
                list.add(instance)
            }
            return list[new Random().next(list.Count-1)];
        }
    }
}


Duoton
2018-08-30 23:19:52
singleton唔係拎來用,concept
你就算copy singleton個template去你段曲度
一樣炒屎, 會return錯value
有機會其他人唔係想get番佢想要既野
2018-08-30 23:23:48
清concept時間

c# + .net

static class test{

static int testfunc(int i){
return i++;
}

}

兩個web request同時call testfunc(1)同testfunc(5)
即係兩條thread同時call佢

咁會唔會本身return 2變咗return 6或任何錯value情況?
2018-08-30 23:26:30
static method冇instance應該唔會炒?
2018-08-30 23:26:57
你個i係arguement 唔係 class variable wor
2018-08-30 23:28:17
會炒,除非有寫Synchronized keyword (java)
2018-08-30 23:28:57

class variable係return memory reference一定會炒

但係argument是咪return value就唔會炒?

真心唔識
2018-08-30 23:29:20
中間啲Operation會炒
2018-08-30 23:30:39
有冇熟c#嘅大大clarify下用static會唔會炒
2018-08-30 23:30:59
lm
2018-08-30 23:32:22
如果我唔係return i++
而係return new k=i++呢?
2018-08-30 23:33:01
Race condition issue
2018-08-30 23:33:56
https://stackoverflow.com/questions/3037637/c-sharp-what-if-a-static-method-is-called-from-multiple-threads

搵到喇
應該唔會炒

ref只要整新ref應該都唔會炒
但新ref會甩咗原本個ref
2018-08-30 23:34:26
直接用argument process 再return點會炒
除非個argument又係一個static object啫
2018-08-30 23:34:41
究竟我地係咪真係咁愛IT, 都11點半ga喇其實
2018-08-30 23:35:31
btw 其實我覺得已經唔係 singleton既問題
已經變左係咪thread safe既問題
2018-08-30 23:36:11
直接argument process
而argument係class reference呢?

唔return new ref是咪會炒
2018-08-30 23:37:01
聽日再睇 發緊燒瞓先
2018-08-30 23:37:02
睇清楚

Variables declared inside methods (with the possible exception of "captured" variables) are isolated, so you won't get any inherent problems; however, if your static method accesses any shared state, all bets are off.

Examples of shared-state would be:

static fields
objects accessed from a common cache (non-serialized)
data obtained via the input parameters (and state on those objects), if it is possible that multiple threads are touching the same object(s)

吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞