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. SCC重新建图

    Tarjan或Kosaraju算法[对每个点归类belong]求出SCC之后,对num_scc个SCC重新建图,针对不同问题,考虑重边的问题. //************************** ...

  2. iOS 阶段学习第八天笔记(指针)

    iOS学习(C语言)知识点整理 一.指针 1)概念:存储变量的地址的一个变量. 2) 数据存储类型分析 1.text (代码段) :存储二进制的可执行代码 2.data(初始化的数据段) 存储初始化的 ...

  3. 通用easyui查询页面组件

    easyui查询页面组件使用指南 本组件开发需求:信息系统的查询页面基本是包括:搜索区域,列表显示区域,按钮条. 1.录入一个查询语句(如:select * from Strudents),录入列表显 ...

  4. iOS取得AddressBook联系人信息

    新建一个CContact类用于存放联系人信息,下面是该类的代码: CContact.h代码: 01 #import <Foundation/Foundation.h> 02   03   ...

  5. kFreeBSD有活过来的迹象?UbuntuBSD

    那些年追过的FreeBSD分支,有debian系的kFreeBSD,arch系的archBSD现在叫pacBSD,gentoo系的gentooBSD:但基本上在虚拟机环境很难成功安装http://ww ...

  6. vim退出后终端保留 退出前的内容

    export TERM=linux 或者 具体选项 可以看看secrecrt的选项

  7. [javascript] 判断 iframe 是否加载完成

    from http://www.planabc.net/2009/09/22/iframe_onload/ var iframe = document.createElement("ifra ...

  8. java web学习总结(一) -------------------基本概念

    一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...

  9. sql server 数据误删找回

    /****** Object: StoredProcedure [dbo].[Recover_Deleted_Data_Proc] Script Date: 04/23/2014 22:11:59 * ...

  10. 我最常用的几个Xcode快键键

    ⌘(command) ⏎(return) ⌥(option/alt) ⇧(shift) ⌃(control/ctrl) 快速打开文件 ⌘ + ⇧ + O(字母) 快速搜索文本 ⌘ + ⇧ + F 分栏 ...