Game hall starts from the Foundation (6)-Go back and elaborate on the edited Year of the maids in the chat room (middle) 1

Source: Internet
Author: User
ArticleDirectory
    •  
    •  
    • Society must progress and the people must begin the revolutionary feudal age

In the previous article, we learned about the behavior patterns of several chat rooms.

Simple and clear push mode Almost no additional language is required to describe its implementation.

In this article, we will look at how the PULL mode is more effective.

This figure clearly shows"Pull"Mode.

    • ConcurrencyMultiple users write data to the data pool
    • ConcurrencyMultiple users read data from the data pool
    • It is best to set the data timeSequenceStored in Collection
    • Enumeration lookup after a certain time will be the largest consumption.

Chat Room Evolution -Maaid's Chronicle Mysterious primitive society

Still refer to our magic simpleAsp3Chat Room

53 Application. Lock
54 Application ("Show5") = Application ("Show4")' A new piece of information drives the fifth piece of information to be eliminated
55  Application ( "Show4" ) = Application ( "Show3" )
56  Application ( "Show3" ) = Application ( "Show2" )
57  Application ( "Show2" ) = Application ( "Show" )
58  Application ( "Show" ) = Newmessage' All other information is moved forward to a new position.
59 Application. Unlock
60 Response. Write application ("Show5")
61 Response. Write application ("Show4")' In PostBack mode, N rows of historical data must be output.
62  Response. Write application ( "Show3" )
63  Response. Write application ( "Show2" )
64  Response. Write application ( "Show" )

Response. write should also be in application. lock block or separate two lock blocks. however, response. write may cause time delay in non-Cache mode. The author painstakingly moves them out of the security lock. in actual operation, it is very likely that you will lose your speech or repeat your speech.

What did the application actually do?The implementation without borders and abstract packaging is like primitive communism.Who is who? Who is it!

Private ownership emerged, Slave Society lock ~ This nvnu belongs to me ~

Translate to C # We can see a logic that is easier to understand.CodeSlightly modify the two locks. They clearly and perfectly arrange the data and threads.

Code snippet

  1. Class Channel
  2. {
  3. Queue<String> Messageq =New Queue<String> ();
  4.  
  5. Public VoidSay (StringMessage)//Write Information
  6. {
  7. Lock(Messageq)
  8. {
  9. Messageq. enqueue (Message );
  10. While(Messageq. Count> 5)//Delete unnecessary
  11. {
  12. Messageq. dequeue ();
  13. }
  14.  
  15. }
  16. }
  17.  
  18. Public String[] Listen ()// \ U-28781?Output all
  19. {
  20. Lock(Messageq)
  21. {
  22. ReturnMessageq. toarray ();
  23.  
  24. }
  25.  
  26. }
  27.  
  28. }

In aspx, it may be called like this.

Code snippet

  1. ChannelCr = session ["Chat"];
  2. Cr. Say (request ["Text"]);
  3.  
  4. Foreach(VaRSInCr. Listen ())
  5. {
  6. Response. Write ("<P>");
  7. Response. Write (s );
  8. Response. Write ("</P>");
  9. }

 

It seems that the task is completed successfully, but it is full of ambiguity.

There is a principle when a transaction or an unmanaged resource accesses a thread critical resource.

You try to release the lock as early as possible,

Let's see what we just did.

Oh my God

Our poor channel, he was locked throughout the process. It seems like a female who has been transferred by the young masters in turn. It really makes me wait for a gentleman to feel surging ~~ Ah, no. It's unbearable.

Since every teacher declares before the transfer that this nvwa is my things! So no one can touch this girl before the teacher declares that she has never missed it!

Lock ~

This is how spectacular a team is if there are so many waits for concurrent access by two masters!

Our servicesProgramIf I write the code according to this efficiency, I am afraid it will crash when the CPU usage is 25%-what is the thread queue's limit? According to Jeffery Richard, when you raised this question, very wrong was taken.

In other words, do not challenge your patience. The consequences are very serious.

 

 

Society must progress and the people must begin the revolutionary feudal age

WeCannot meetSuchPerformance.

We understand that the young master did not observe this when being transferred to the nvwa (add lock when writing to prevent reading by other threads)

When the young master and the maids drink tea, the old master cannot mess in and start the restaurant for training. We also accept this (add lock when reading to prevent other threads from writing)

 

But the young masters are looking for nvwa to drink tea. There is no reason not to join the tea conference!

In a sense, as long as the set elements remain unchanged, the queue object supports Secure concurrent reads. Why are several threads reading data, what if we continue to lock each other and reject each other? We only have love for the maids. Nothing can be fair!

Changing a lock from a full lock to a read/write lock can effectively reduce unnecessary waiting. -- We can shorten the tea drinking team!

 

Code snippet
  1. Class Channelreadwritelock
  2. {
  3. Queue<String> Messageq =New Queue<String> ();
  4. System. threading.Readerwriterlockslim_ Lock =NewSystem. threading.Readerwriterlockslim();
  5. Public VoidSay (StringMessage)// Write information
  6. {
  7. _ Lock. enterwritelock ();
  8. Messageq. enqueue (Message );
  9. While(Messageq. Count> 5)// Delete unnecessary
  10. {
  11. Messageq. dequeue ();
  12. }
  13. _ Lock. exitwritelock ();
  14. }
  15. Public String[] Listen ()// \ U-29701? All
  16. {
  17. _ Lock. enterreadlock ();
  18. VaRAry = messageq. toarray ();
  19. _ Lock. exitreadlock ();
  20. ReturnAry;
  21. }
  22. \

This is the victory of the maids. She is no longer a person (person), but an ordinary person who can communicate with SoCal. Although still in the shadows of the feudal parent-teacher system, she is still the underfloor people who have been exploited and abused, but she already has more freedom than before!

 

Write it here to see the basis of Harmony. Please wait for the definition of the Year 2 by the maids.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.