日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C#复习⑧

發布時間:2025/3/15 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#复习⑧ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#復習⑧

2016年6月22日

13:50

Main Attribute & Threads 屬性與線程

1.Conditional Attribute 條件屬性

#define debug // preprocessor directiveclass C {[Conditional("debug")] // only possible for void methodsstatic 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 methodspublic static void Sleep(int milliSeconds) {...}...public Thread(ThreadStart startMethod) {...} // thread creationpublic string Name { get; set; } // propertiespublic ThreadPriority Priority { get; set; }public ThreadState ThreadState { get; }public bool IsAlive { get; }public bool IsBackground { get; set; }...public void Start() {...} // methodspublic void Suspend() {...}public void Resume() {...}public void Join() {...} // t.Join(): caller waits for t to diepublic void Abort() {...} // throws ThreadAbortExceptionpublic void Interrupt() {...} // callable in WaitSleepState ...}public delegate void ThreadStart(); // parameterless void methodpublic 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-- finallydone*/

8.互斥Mutual Exclusion

一次只能有一個線程掌握著該鎖。直到該鎖被釋放才能被其他線程調用。

舉例:

class Account { // this class is a monitorlong 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.

舉例:

?

轉載于:https://www.cnblogs.com/zpfbuaa/p/5606974.html

總結

以上是生活随笔為你收集整理的C#复习⑧的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。