多執行緒技術應用(一)

2021-08-28 04:12:28 字數 2516 閱讀 1277

dyt_jcbk(1/900:2018/9/16)

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading;

namespace threadtest

);//thread thread2 = new thread(() => console.writeline("我是通過lambda表示式建立的委託"));

#endregion

#region 3.parameterizedthreadstart是乙個有參的,返回值為void的委託,委託引數的型別必須是object的

//thread thread = new thread(new parameterizedthreadstart(thread1));

"這是乙個有引數的委託");

#endregion

#region  4.執行緒的常用方法

/*abort() 終止執行緒;

getdomain() 返回當前執行緒正在其中執行的當前域

getdomainid() 返回當前執行緒正在其中執行的當前域id

join() 已過載.阻塞呼叫執行緒,直到某個執行緒終止時為止

interrupt() 中斷處於waitsleepjoin終止時為止

resume() 繼續執行已經掛起的執行緒

start() 執行本執行緒

suspend() 掛起當前執行緒,如果當前執行緒處於已經掛起的狀態則不起作用

sleep() 把正在執行的執行緒掛起一段時間

*/#endregion

#region 5.thread的方法練習

獲取正在執行的執行緒

//thread thread = thread.currentthread;

設定執行緒的名字

= "main thread";

獲取當前執行緒的唯一識別符號

//int id = thread.managedthreadid;

獲取當前執行緒的狀態

//threadstate state = thread.threadstate;

//threadpriority priority = thread.priority;

//string msg = string.format("thread id:\n" + "thread name:\n" + "thread state:\n" + "thread priority:\n", id, thread.name, state, priority);

#endregion

#region 6.前台執行緒和後台執行緒

/*** 前台執行緒:只有所有的前台執行緒都結束,應用程式才能結束,預設情況下建立的執行緒都是前台執行緒

* 後台執行緒:只要所有的前台執行緒結束,後台執行緒自動結束,通過thread>isbackground設定後台執行緒,必須在start()方法之前設定執行緒型別,因為一旦執行,無法

* 改變型別

* /*/

//演示先後臺執行緒

backgroundtest background = new backgroundtest(10);

//建立前台執行緒

thread fthread = new thread(new threadstart(background.runloop));

//給執行緒命名

fthread.name = "前台執行緒";

backgroundtest background1 = new backgroundtest(20);

//建立後台執行緒

thread bthread = new thread(new threadstart(background1.runloop));

bthread.name = "後台執行緒";

//設定為後台執行緒

bthread.isbackground = true;

//啟動執行緒

fthread.start();

bthread.start();

#endregion

}#region 1.無參的呼叫方法

//public static void thread1()

//#endregion

#region 3.parameterizedthreadstart是乙個有參的,返回值為void的委託

//public static void thread1(object obj)

//#endregion

}#region 6.前台執行緒和後台執行緒

class backgroundtest

public void runloop()

計數:", threadname, i.tostring());

//執行緒休眠500ms

thread.sleep(500);

}console.writeline("完成計數", threadname);}}

#endregion

}

多執行緒技術學習 一

1.什麼是執行緒?什麼是多執行緒?2.執行緒出現在 3.如何使用執行緒?1.什麼是執行緒?我不想像教科書寫的那樣理解,站在作業系統的角度,執行緒就是能夠被cpu排程的最小執行單元.說到這裡不得不說程序,在windows中.開啟任務管理器看見的那些.exe都是程序.程序占有系統資源.有 空間,有記憶體...

多執行緒技術 一 執行緒概述

程序 是應用程式的乙個執行例程,是應用程式的一次動態執行過程。執行緒 是程序中的乙個執行單元 是作業系統分配cpu時間的基本單元。windows是乙個支援多執行緒的系統。乙個程序可以包含若干個執行緒。多執行緒的概念 多執行緒 在同一時間執行多個任務的功能,稱為多執行緒或自由執行緒。多執行緒的優點 可...

多執行緒技術

1,程序 執行緒 程序 系統中同時執行的不同程式 執行緒 程式中同時執行不同的操作 單個cpu只能按順序執行指令,cpu可以隨機在不同的程序和執行緒進行切換,保證程序和執行緒都執行一遍後再重複這個過程。因為cpu執行速度足夠快,讓人感覺程式是同時執行的。2,執行緒 thread thread sle...