C#复习⑧

2016年6月22日

13:50

Main Attribute & Threads 属性与线程

1.Conditional Attribute 条件属性

#define debug         // preprocessor directive

class C {

[Conditional("debug")]        // only possible for void methods

static void Assert (bool ok, string errorMsg) {

if (!ok) {

Console.WriteString(errorMsg);

System.Environment.Exit();

}

}

static void Main (string[] arg) {

Assert(arg.Length > , "no arguments specified");

Assert(arg[] == "...", "invalid argument");

...

}

}

断言仅被调用,如果定义了debug。

Assert is only called, if debug was defined.

还可用于控制跟踪输出。

Also useful for controlling trace output.

2.Serialization 序列化

3.AttributeUsage

定义自己的Attribute:

4.线程

声明一个线程:

//假设有方法void M(){}

Thread t = new Thread(M);

t.Start(); //线程执行

5.Type类型

public sealed class Thread {

public static Thread CurrentThread { get; }        // static properties and methods

public static void Sleep(int milliSeconds) {...}

...

public Thread(ThreadStart startMethod) {...}        // thread creation

public string Name { get; set; }        // properties

public ThreadPriority Priority { get; set; }

public ThreadState ThreadState { get; }

public bool IsAlive { get; }

public bool IsBackground { get; set; }

...

public void Start() {...}        // methods

public void Suspend() {...}

public void Resume() {...}

public void Join() {...}        // t.Join(): caller waits for t to die

public void Abort() {...}        // throws ThreadAbortException

public void Interrupt() {...}        // callable in WaitSleepState

...

}

public delegate void ThreadStart();        // parameterless void method

public enum ThreadPriority {Normal, AboveNormal, BelowNormal, Highest, Lowest}

public enum ThreadState {Unstarted, Running, Suspended, Stopped, Aborted, ...}

举例:

using System;

using System.Threading;

class Printer {

char ch;

int sleepTime;

public Printer(char c, int t) {ch = c; sleepTime = t;}

public void Print() {

for (int i = ; i < ; i++) {

Console.Write(ch);

Thread.Sleep(sleepTime);

    }

  }

}

class Test {

static void Main() {

Printer a = new Printer('.', );

Printer b = new Printer('*', );

new Thread(a.Print).Start();

new Thread(b.Print).Start();

  }

}

6.与Java的不同之处

7.线程的状态以及相互转化

using System;

using System.Threading;

class Test {

static void P() {

for (int i = ; i < ; i++) {

Console.Write('-');

Thread.Sleep();

  }

}

static void Main() {

Thread t = new Thread(P);

Console.Write("start");

t.Start();

t.Join(); // waits until t has finished

Console.WriteLine("end");

  }

}

//Output

// start--------------------end
using System; using System.Threading;

class Test {

static void P() {

try {

try {

try {

while (true) ;

  } catch (ThreadAbortException) { Console.WriteLine("-- inner aborted"); }

    } catch (ThreadAbortException) { Console.WriteLine("-- outer aborted"); }

      } finally { Console.WriteLine("-- finally"); }

}

static void Main(string[] arg) {

Thread t = new Thread(P);

t.Start(); Thread.Sleep();

t.Abort(); t.Join(); Console.WriteLine("done");

  }

}

/*Output

-- inner aborted

-- outer aborted

-- finally

done*/

8.互斥Mutual Exclusion

一次只能有一个线程掌握着该锁。直到该锁被释放才能被其他线程调用。

举例:

class Account {        // this class is a monitor

long val = ;

public void Deposit(long x) {

lock (this) { val += x; }        // only 1 thread at a time may execute this statement

}

public void Withdraw(long x) {

lock (this) { val -= x; }

  }
}

锁可以加在任何类型上:

object semaphore = new object();

...

lock (semaphore) { ... critical region ... }

9.Wait and Pulse

Monitor.Wait(lockedVar);       // 大致等于wait() in Java (in Java lockedVar is always this)

Monitor.Pulse(lockedVar);        //大致等于 notify() in Java

Monitor.PulseAll(lockedVar);       // 大致等于 notifyAll() in Java

举例:

PulseAll(v)唤醒所有等待的线程的V,但其中只有一个是允许继续。其他线程必须等待,直到前一个释放了锁。然后,下一个线程可能进入执行。

PulseAll(v) wakes up all threads that wait for v, but only one of them is allowed to continue. The others must wait until the previous one has released the lock. Then the next thread may enter the critical region.

举例:

C#复习⑧的更多相关文章

  1. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  2. vuex复习方案

    这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.

  3. 我的操作系统复习——I/O控制和系统调用

    上篇博客介绍了存储器管理的相关知识——我的操作系统复习——存储器管理,本篇讲设备管理中的I/O控制方式和操作系统中的系统调用. 一.I/O控制方式 I/O就是输入输出,I/O设备指的是输入输出设备和存 ...

  4. 复习(1)【Maven】

    终于开始复习旧知识了,有输入必然要有输出.输入和输出之间的内化过程尤为重要,在复习的同时,真正把学到的东西积淀下来,加深理解. Maven项目概念与配置 Maven是一个项目管理和综合工具.Maven ...

  5. 《CSS权威指南》基础复习+查漏补缺

    前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...

  6. JS复习--更新结束

    js复习-01---03 一 JS简介 1,文档对象模型 2,浏览器对象模型 二 在HTML中使用JS 1,在html中使用<script></script>标签 2,引入外部 ...

  7. jQuery 复习

    jQuery 复习 基础知识 1, window.onload $(function(){});   $(document).ready(function(){}); 只执行函数体重的最后一个方法,事 ...

  8. jQuery5~7章笔记 和 1~3章的复习笔记

    JQery-05 对表单和表格的操作及其的应用 JQery-06 jQuery和ajax的应用 JQery-07 jQuery插件的使用和写法 JQery-01-03 复习 之前手写的笔记.实在懒得再 ...

  9. HTML和CSS的复习总结

    HTML(Hypertext Markup Language)超文本标记语言:其核心就是各种标记!<html> HTML页面中的所有内容,都在该标签之内:它主要含<head>和 ...

  10. 2017年1月1日 java学习第二天复习

    今天是新年的第一天,以前学习没有总结习惯,学习效率和成果都很不好.  学习的过程就是反复的复习和不断学习的过程,开始今天的学习总结   学习java的第二天. 今天学习了java最基础的一些内容,照着 ...

随机推荐

  1. JS虚拟键盘

    由于是触摸屏,所以需要一款JS虚拟键盘.上网找了一个好用的VirtualKeyboard,作了修改. 修改该插件参考的博客文章:http://www.cnblogs.com/xinggong/arch ...

  2. 用c#开发微信 (4) 基于Senparc.Weixin框架的接收事件推送处理 (源码下载)

    本文讲述使用Senparc.Weixin框架来快速处理各种接收事件推送.这里的消息指的是传统的微信公众平台消息交互,微信用户向公众号发送消息后,公众号回复消息给微信用户.包括以下类型: 1 subsc ...

  3. android 中layer-list的用法

    1.可以将多个图片按照顺序层叠起来 2.在drawable下建立一个xml文件 <layer-list xmlns:android="http://schemas.android.co ...

  4. 介绍开源的.net通信框架NetworkComms框架 源码分析(十二)PriorityQueue

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  5. 【译】java.lang.ThreadLocal

    This class provides thread-local variables. These variables differ from their normal counterparts(副本 ...

  6. C#DateTimePicker控件问题

    DateTimPicker控件在遇到29这样特殊的日期,选择时可能会出现 解决方案:在属性中把Value值设置为除29日外的其他日期或者在代码中直接设置Value值:DateTimePicker1 = ...

  7. 孙鑫MFC学习笔记14:网络编程

    1.OSI 2.TCP/IP与OSI对应关系 3.Socket 4.客户机/服务器模式 5.Windows Sockets 6.套接字类型 7.面向连接的socket编程 8.面向无连接的socket ...

  8. Mysql存储过程(四)——异常处理

    http://blog.csdn.net/crazylaa/article/details/5368421 有时候,不希望存储过程抛出错误中止执行,而是希望返回一个错误码. MySQL 支持异常处理, ...

  9. PHP中用GD绘制饼图

    PHP中用GD绘制饼图,绘制的类见代码: Class Chart{ private $image; // 定义图像 private $title; // 定义标题 private $ydata; // ...

  10. 后缀名为properties,config和xml的文件内容读取

    1.先建立文件(后缀名为properties和config) 2.读取类建立 public class Read{ public static Properties  properties = new ...