Singleton design pattern C # implementation (below)

zhaozj2021-02-16  28

(Connected to the part)

The following is the main function, the test entry of this program:

Using system;

Namespace cspattern.singleton

{

Public Class Runmain

{

Public runmain () {}

Static public void

Main

(String [] ARGS)

{

MutileThread.mutileClient myclient = new mutilethread.mutileclient ();

Myclient.clientmain ();

System.console.readline ();

}

}

}

The result is as follows:

Thread Thread 1 Report: Current Counter is: 2

Thread Thread 1 Report: Current Counter is: 4

Thread Thread 1 Report: Current Counter is: 5

Thread Thread 1 Report: Current Counter is: 6

Thread Thread 3 Report: The current Counter is: 7

Thread Thread 3 Report: Current Counter is: 8

Thread Thread 3 Report: Current Counter is: 9

Thread Thread 3 Report: Current Counter is: 10

Thread Thread 0 Report: Current Counter is: 1

Thread Thread 0 Report: Current Counter is: 11

Thread Thread 0 Report: Current Counter is: 12

Thread Thread 0 Report: Current Counter is: 13

Thread Thread 2 Report: Current Counter is: 3

Thread Thread 2 Report: Current Counter is: 14

Thread Thread 2 Report: Current Counter is: 15

Thread Thread 2 Report: Current Counter is: 16

Due to the different system threads, each execution result is different, but the final result must be 16.

Method One is created since the example is started, so the instance () method does not need to decide whether there is a unique instance, and returns the instance, so there will be many instantiated issues of counter classes.

Method 2:

Using system;

Using system.threading;

Using system.Runtime.compilerServices;

Namespace cspattern.singleton

{

Public Class Counter_Lazy

{

Static Counter_Lazy Unicounter;

Private int totnum = 0;

Private counter_lazy ()

{

Thread.sleep (100); // assumes that it is blocked for some reason for some reason for some reason for 100 milliseses

}

[MethodIMPL (MethodImploptions.Synchronized] // Synchronization Properties

Static Public Counter_Lazy Instance ()

{

IF (null == unicounter)

{

UNICOUNTER = New Counter_Lazy ();

}

Return Unicounter;

}

Public void inc () {Totnum ;}

Public int getCounter () {Return Totnum;}

}

}

I don't know if you have noticed the [MethodImpl (MethodImploptions.synchronized] statement above the instance () method, he is the point of synchronization, he specifies the instance () method can only be used by a thread, so that thread 0 is avoided. Call instance () Creating a completion instance Front thread 1 to call instance () attempt to obtain this instance.

According to the MSDN prompt, you can also use the Lock keyword to lock the thread, the code is as follows: use system;

Using system.threading;

Namespace cspattern.singleton

{

Public Class Counter_Lazy

{

Static Counter_Lazy Unicounter;

Static Object MyObject = New Object ();

Private int totnum = 0;

Private counter_lazy ()

{

Thread.sleep (100); // assumes that it is blocked for some reason for some reason for some reason for 100 milliseses

}

Static Public Counter_Lazy Instance ()

{

LOCK (MyObject)

{

IF (null == unicounter)

{

UNICOUNTER = New Counter_Lazy ();

}

Return Unicounter;

}

}

Public void inc () {Totnum ;}

Public int getCounter () {Return Totnum;}

}

}

LOCK () is a mutually exclusive lock for an object, only one thread is allowed to access the braces in the braces until the code execution is unlocked, and the other threads are allowed to perform their clause blocks.

You can also synchronize using the Mutex class, define private static mutex mut = new mutex (), modifying instance () as follows, the same results can be obtained:

Static Public Counter_Lazy Instance ()

{

Mut.waitone ();

IF (null == unicounter)

{

UNICOUNTER = New Counter_Lazy ();

}

Mut.releasemutex ();

Return Unicounter;

}

Note that in this example, use the method of the method to change the client, remove the comment of counter_lazy.intance () and annotate the counter.Inceance ().

Singleton mode can also be expanded, as long as you modify it, you can limit only the M instances in an application, and provide a global transparent access method for M instances.

转载请注明原文地址:https://www.9cbs.com/read-27790.html

New Post(0)