C#复习⑧
2016-06-22 14:24
537 查看
C#复习⑧
2016年6月22日
13:50
Main Attribute & Threads 属性与线程
1.Conditional Attribute 条件属性
断言仅被调用,如果定义了debug。
Assert is only called, if debug was defined.
还可用于控制跟踪输出。
Also useful for controlling trace output.
2.Serialization 序列化
3.AttributeUsage
定义自己的Attribute:
4.线程
声明一个线程:
5.Type类型
举例:
6.与Java的不同之处
7.线程的状态以及相互转化
8.互斥Mutual Exclusion
一次只能有一个线程掌握着该锁。直到该锁被释放才能被其他线程调用。
举例:
锁可以加在任何类型上:
object semaphore = new object();
...
lock (semaphore) { ... critical region ... }
9.Wait and Pulse
举例:
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.
举例:
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(0); } } static void Main (string[] arg) { Assert(arg.Length > 0, "no arguments specified"); Assert(arg[0] == "...", "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 = 0; i < 100; i++) { Console.Write(ch); Thread.Sleep(sleepTime); } } } class Test { static void Main() { Printer a = new Printer('.', 10); Printer b = new Printer('*', 100); 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 = 0; i < 20; i++) { Console.Write('-'); Thread.Sleep(100); } } 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(0); 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 = 0; 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#中控制不能从一个case标签贯穿到另一个case标签
- C#复习⑦
- C#里的SubString和Convert.ToDateTime
- 【C# 计算代码的执行时间】
- C#中split用法实例总结
- c# toolstrip控件怎么把左边的几个小点去掉??
- C#实现DataSet内数据转化为Excel和Word文件的通用类完整实例
- C#复习⑥
- C# IEnumerable、IEnumerator和foreach的联系与解析
- C#使用winform简单导出Excel的方法
- C# button 去边框
- C#控制Excel Sheet使其自适应页宽与列宽的方法
- c#取得应用程序根目录
- c# sleep 例子
- C# System.IO命名空间常用的类
- C#金额小写变大写
- c# mouseenter mousemove区别?
- c# 添加了按钮双击事件后,再删除掉代码会提示错误
- C#中高效的数据插入方法
- C#动态调用webservice