C#多线程学习笔记(三)

刚刚把前两天的笔记整理完了,发现做笔记可以加深印象。要坚持做下去,可以学到一些细节的东西。

a.今天学到一个非常试用的lock
  语法:
   lock(expression) statement_block
   expression代表你希望跟踪的对象,通常是对象引用。一般地,如果你想保护一个类的实例,你可以使用this;如果你希望保护一个静态变量(如互斥代码段在一个静态方法内部),一般使用类名就可以了。而statement_block就是互斥段的代码,这段代码在一个时刻内只可能被一个线程执行。
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Threading;
None.gif
None.gif
namespace LockThread
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
internal class Account
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int balance;
InBlock.gif        Random r 
= new Random();
InBlock.gif        
internal Account(int _initial)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            balance 
= _initial;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal int Withdraw(int _amount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(balance < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//如果balance<0则抛出异常
InBlock.gif
                throw new Exception("Negative Balance");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//下面的代码保证在当前线程修改balance的值完成之前
InBlock.gif            
//不会有其他线程也执行这段代码来修改balance的值
InBlock.gif            
//因此,balance的值是不可能小于0的
InBlock.gif
            lock(this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Current Thread:"+Thread.CurrentThread.Name);
InBlock.gif                
//如果没有lock关键字的保护,那么可能在执行完if的条件判断之后
InBlock.gif            
//另外一个线程却执行了balance=balance-amount修改了balance的值
InBlock.gif            
//而这个修改对这个线程是不可见的,所以可能导致这时if的条件已经不成立了
InBlock.gif            
//但是,这个线程却继续执行balance=balance-amount,所以导致balance可能小于0
InBlock.gif
                if(balance >= _amount)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Thread.Sleep(
5);
InBlock.gif                    balance 
= balance -_amount;
InBlock.gif                    
return _amount;
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 elsedot.gif{
InBlock.gif                    
return 0;//处理事务被拒绝
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
internal void DoTransactions()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int i = 0;i<100; i++)
InBlock.gif                Withdraw(r.Next(
-50100));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif 
InBlock.gif
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static internal Thread[] threads = new Thread[10];
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Account acc 
= new Account(0);
InBlock.gif            
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Thread t 
= new Thread(new ThreadStart(acc.DoTransactions));
InBlock.gif                t.Name 
= i.ToString();
InBlock.gif                threads[i] 
= t;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                threads[i].Start();
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


b.还有一个Moniter对象是用来监视对象的,
......
Queue oQueue=new Queue();
......
Monitor.Enter(oQueue);
......//现在oQueue对象只能被当前线程操纵了
Monitor.Exit(oQueue);//释放锁

上面表示oQueue这个对象只有一个线程可以操纵,只有当Mointor.Exit才可以被其它线程所操纵

转载于:https://www.cnblogs.com/DKSoft/articles/569123.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380488.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux ida 图形界面,linux – IDA在屏幕内不起作用

我试图在屏幕会话中运行idal64(IDA pro),但是我收到此错误&#xff1a;TVision error: Can not load libcurses.soWithout libcurses can work only with xterm/linuxAborted (core dumped)我安装了’libncurses5-dev’,’libncursesw5-dev’,’lib32ncurses5-dev’和’libx32n…

C和汇编混合编程----printf

今天终于用c和汇编成功调试出第一个程序了&#xff0c;程序很简单&#xff0c;我太菜了&#xff0c;花了几天的时间&#xff0c;才调试好&#xff0c;来记录一下&#xff0c;以防忘记了 先上程序&#xff1a; #include "stdio.h" int main() {char *str"begin\…

(诡异事件)iframe标签后面的alert不执行

今天做项目的过程中&#xff0c;发现一个非常奇怪的事情。iframe标签后面的js 不执行&#xff1f; 把代码贴出来&#xff0c;看看有没有大牛碰到这种情况。谢谢。 <html xmlns"http://www.w3.org/1999/xhtml"><head> <title>Untitled Page</…

kotlin 判断数字_Kotlin程序检查给定数字是正数,负数还是零

kotlin 判断数字A positive number is a number which is greater than 0, a negative number is a number which is less than 0, else the number is Zero. 正数是大于0的数字&#xff0c;负数是小于0的数字&#xff0c;否则为零。 Given a number num, we have to check wh…

gz格式linux怎么打开,linux 下载解压gz文件怎么打开

一般用WINRAR下载地址&#xff1a;http://www.onlinedown.net/soft/5.htm如何下载安装winrar并打开“压缩文件”如何下载、安装winrar&#xff0c;并打开“压缩文件”网上有很多文件都是压缩格式&#xff0c;经过压缩后的文件由于其体积较小&#xff0c;因而比较适合网络传输&a…

c和汇编混合编程----main的反汇编

想看一下main函数的反汇编程序&#xff0c;分析一下&#xff1a; int main() {return 0; } 反汇编&#xff1a; 1: int main() 2: { 00401010 55 push ebp &#xff1b;将ebp压入栈 00401011 8B EC mov ebp,esp &am…

转 商业软件编程很无聊?

这周读到三篇博客帖子。把它们串在一块儿读&#xff0c;对我们的职业发展非常有教育意义。 一篇是Thoughtworks前员工Ravi Mohan写的&#xff0c;《但是马老大&#xff0c;商业编程就是无聊》。Martin Fowler在一篇帖子里说&#xff0c;编写企业软件不光是捣腾数据。并不是只有…

外中断02 - 零基础入门学习汇编语言70

第十五章&#xff1a;外中断02 让编程改变世界 Change the world by program 小甲鱼和大家谈谈心 一个帖子引发小甲鱼的反省&#xff01; 猫姐曾经说过&#xff0c;步子别迈太大&#xff0c;容易扯着蛋&#xff01; 结果还真蛋疼了…… 因此&#xff0c;小甲鱼要学会淡定…

蛇形填数

描述 在n*n方阵里填入1,2,…,n*n,要求填成蛇形。例如n4时方陈为&#xff1a; 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 输入 直接输入方陈的维数&#xff0c;即n的值。(n<100) 输出 输出结果是蛇形方陈。 样例输入 3 样例输出 7 8 1 6 9 2 5 4 3 代码如下 …

c#数组获取元素的索引_获取元素集合 从C#中的指定索引

c#数组获取元素的索引Given a Collection<T> of integer types, and an index, we have to access the element from the given index. 给定一个整数类型的Collection <T>和一个索引&#xff0c;我们必须从给定索引访问元素。 To access an element of the Collec…

linux清理缓存cache,Linux清理cache缓存

当cache缓存占用太大&#xff0c;服务起不来&#xff0c;需要查看清理缓存查看缓存&#xff1a;free -m输入运行下面一行&#xff1a;echo 3 > /proc/sys/vm/drop_caches# 释放缓存区内存的方法1)清理pagecache(页面缓存)# echo 1 > /proc/sys/vm/drop_caches 或者 # sys…

c和汇编混合编程----shellcode----弹出计算器

先用c和汇编混合编程成功弹出计算器&#xff08;在VC里运行&#xff09; #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoa…

[导入]转:世界小姐形容他们国家male organ

世界小姐眼中的最后一问&#xff1a;形容一下他们国家male organMiss Universes last Question 环球小姐最后一题 Question: Ms America, how do you describe a male organ in your country? 问&#xff1a;美国小姐&#xff0c;请形容贵国男性的性器官。Ms America: Well, I…

scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

scala字符串替换Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string) Replacing a part of the string that matches our given regular expression can be done using multiple methods. 可以使用多种方法替换匹配给定正则表…

有趣的数

描述 把分数按下面的办法排成一个数表。 ① ② ⑥ ⑦ 1/1 1/2 1/3 1/4… ③ ⑤ ⑧ 2/1 2/2 2/3… ④ ⑨ 3/1 3/2 … ⑩ 4/1… … 我们以z字型方法给上表的每项编号。特定方法&#xff1a;第一项是1/1&#xff0c;然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。编程输入项号N&…

linux系统的层次结构,关于Linux操作系统层次结构分析

本文转自http://www.jb51.net/LINUXjishu/214104.html首先来看一张图(这是Linux操作系统的大致层次结构)&#xff1a;最内层是硬件&#xff0c;最外层是用户常用的应用&#xff0c;比如说firefox浏览器&#xff0c;evolution查看邮件&#xff0c;一个计算流体模型等等。硬件是物…

Windows Vista版本比较

Windows Vista上市在即&#xff0c;对许多朋友来说目前急待解决的问题便是&#xff0c;在升级或购买时应该选择Windows Vista的哪个版本&#xff0c;哪个版本更契合自己的应用需求&#xff0c;究竟应该选择Windows Vista Home呢还是Windows Vista Ultimate?等等。 之前在Windo…

shellcode---c和汇编混合编程---弹出cmd

首先用C/C语言实现弹出cmd #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoadLibrary(dll);WinExec("cmd.exe",S…

ajax 复制到“剪贴板”

有时候可能会做一些“复制”按钮的功能&#xff0c;当用户点击“复制”按钮时&#xff0c;就会将要复制的内容复制出来&#xff0c;以下代码即实现“复制”按钮的功能。该功能需要用到AJAX的PageMethods来调用页面后台代码来实现。 调用PageMethods&#xff0c;需要引用 <sc…

找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串

找到最大回文子串Problem statement: 问题陈述&#xff1a; Given a string, you have to find the largest palindromic substring using O(1) space complexity. 给定一个字符串&#xff0c;您必须使用O(1)空间复杂度找到最大的回文子字符串。 Input:T Test caseT no of in…