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. C# HTTP上传文件

    代码: /// <summary> /// Http上传文件 /// </summary> public static string HttpUploadFile(string ...

  2. STOP:c0000218 {Registry File Failure}

    这几天够折腾的了,一台很老很老的服务器,在启动之后,一个阵列磁盘掉线了: 也许是磁盘坏了: 服务器4个硬盘做的是RAID1,正常来说,坏了其中一二个硬盘是没有问题的.现更换了一个好的硬盘之后,系统无法 ...

  3. java开源时间/日期库Joda-Time

    任何企业应用程序都需要处理时间问题.应用程序需要知道当前的时间点和下一个时间点,有时它们还必须计算这两个时间点之间的路径.使用 JDK 完成这项任务将非常痛苦和繁琐.现在来看看 Joda Time,一 ...

  4. 【UWP】FlipView绑定ItemsSource,Selectedindex的问题

    最近在做列表头部的Carousel展示,Carousel使用的是FlipView展示,另外使用ListBox显示当前页,如下图 我们先设置一个绑定的数据源 public class GlobalRes ...

  5. matlab 调用dos命令和文件操作

    第一.利用!直接调用,简单方便,可以带操作对象:!del A.bat 第二.调用system函数或者dos函数,既可以实现功能,又返回参数,能检查执行情况,方便后面程序的开发,推荐这个 [status ...

  6. ASP.NET页面中去除VIEWSTATE视

    保存页的所有视图状态信息和控件状态信息. 源码:http://www.jinhusns.com/Products/Download/?type=xcj 作者在早期参与的项目中曾遇到这样的需求:基于SE ...

  7. 【C#进阶系列】17 委托

    委托主要是为了实 现回调函数机制,可以理解为函数指针(唯一不同的在于多了委托链这个概念). 然而用的时候可以这么理解,但是委托的内部机制是比较复杂的. 一个委托的故事 delegate void ra ...

  8. 孙鑫MFC学习笔记7:对话框编程(上)

    1.DoModal创建模态对话框 2.Create创建非模态对话框(需要用ShowWindow显示出来) 模态:对话框显示,程序会暂停,直到对话框关闭 非模态:对话框显示,程序继续执行 3.对于模态对 ...

  9. XE8 FMX SpeedButton 大图标(改 Style)

    自从 XE8 提供 ImageList 带来了很多便利,但 SpeedButton 的图标太小(不够大气),还好 FMX 提供了 Style 可供使用者自订图标大小及显示位置,请自行按图索骥,做一遍: ...

  10. Java--Spring AOP 源码散点记录(最后整理成一篇博客)

    Spring AOP 源码记录: 1.AOP 入口ProxyFactoryBean.getObject()方法: 2.AOP实现: (1)实现InvocationHandler接口 (2)通过java ...