Summary
These two days have been thinking about Redis queue: A producer, multiple consumers, here's a demo to test.
An example
Refer to the previous article on how to refer to Redisclient: the Redis practice of C # list,hashtable
Producer a thread, and then turn on multiple threads to consume data.
The code is as follows:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;UsingNservicekit.redis;UsingNservicekit.serviceclient;UsingSystem.Threading;Namespaceredisdemo{Classprogram {Staticvoid Main (String[] args) {Thread thread =NewThread (run); Thread. Start (); thread[] Threads =New thread[10];for (int i =0; I < threads. Length; i++) {Threads[i] =NewThread (pull); Threads[i]. Start (); } console.read (); }PrivateStaticvoidPull () {Iredisclientfactory factory =Redisclientfactory.instance;using (iredisclient client = factory. Createredisclient ("192.168.1.37",6379)) {client. Password ="Wolfy";while (True) {if (client. GetListCount ("Myqueue") >0) {string result = client. Dequeueitemfromlist ("Myqueue");//Suspends the current thread if the obtained content is empty 1sif (String. IsNullOrEmpty (Result)) {thread.spinwait (1000); }Else{Console.WriteLine ("Threadid:"+ Thread.CurrentThread.ManagedThreadId.ToString () +"\ t" +result); } }Else{//If the current queue is empty, suspend 1s thread.spinwait (1000); } } } }PrivateStaticvoid run () {iredisclientfactory factory = redisclientfactory.instance; using (iredisclient client = factory. Createredisclient ( "192.168.1.37 ", 6379 "wolfy" ; while (true "myqueue "
Test
Summarize
The queue has been considered rabbitmq,msmq and so on, considering that the company has a ready-made Redis server, so consider using Redis queues. Since the implementation of a producer, multiple consumers, then next, want to implement a multi-queue, and then set the capacity of the queue, through the capacity, the producer in the queue, depending on whether the queues are full, and then the data distribution situation.
Reprint: Blog Address: http://www.cnblogs.com/wolf-sun/
Redis Queue of C #