執行緒:
using system;
using system.collections.generic;
using system.text;
using system.threading;
namespace 執行緒thread
public void threadmain()
", data);}}
static void main(string args)
", thread.currentthread.managedthreadid));
t1.start();
console.writeline("this is the main thread ,id :", thread.currentthread.managedthreadid);
*/ //給執行緒傳遞資料 方法1
/*var d = new data ;
var t2 = new thread(threadmainwithparameters);
t2.start(d);
*///給執行緒傳遞資料 方法2
/*var obj = new mythread("info");
var t3 = new thread(obj.threadmain);
t3.start();
*///後台執行緒 測試後台執行緒請去掉 console.read();
var t1 = new thread(threadmain) ;
t1.start();
console.writeline("main thread ending now.");
// console.read();
}public struct data
static void threadmainwithparameters(object o)
", d.message);
} static void threadmain()
started", thread.currentthread.name);
thread.sleep(3000);
console.writeline("thread completed", thread.currentthread.name);
"running in a thread.");} }
}
執行緒池:
using system;
using system.collections.generic;
using system.text;
using system.threading;
namespace 執行緒池
," +
"i/o completion threads:",
nworkerthreads, ncompletionportthreads);
for (int i = 0; i < 5; i++)
console.read();
}static void jobforathread(object state)
,running inside pooled thread ", i, thread.currentthread.managedthreadid);
thread.sleep(50);}}
}}
執行緒同步:
執行緒同步:using system;
using system.collections.generic;
using system.text;
using system.threading;
namespace 執行緒同步
}set}}
}public class task
public void dothetask()}}
}static void main1(string args)
for (int i = 0; i < numthreads; i++)
console.writeline("summarized ", sharedstate.state);
console.read();} }
}
using system;
using system.collections.generic;
using system.text;
using system.threading;
namespace 執行緒同步
}private int decrement = 0;
/// /// 這裡是decrment的例項
///
public int decrement
", decrement);
interlocked.decrement(ref decrement);
console.writeline("呼叫interlocked.decrement後的結果為", decrement);
return decrement;}}
private int exchange = 0;
/// /// 這裡是exchange的例項
///
public int exchange
", exchange);
interlocked.exchange(ref exchange, 10);
console.writeline("呼叫interlocked.exchange後的結果為", exchange);
return exchange;}}
}public class task
public void tothetask()}}
static void main2()
for (int i = 0; i < numthreads; i++)
console.writeline("結果 summarized ", state.state - 1);
console.writeline("結果 +1 summarized ", state.state);
console.writeline("呼叫2次後結果summarized ", state.state);
int itest= state.decrement;
itest = state.exchange;
console.read();}}
}
多執行緒同步與執行緒池
前台執行緒和後台執行緒 c 中的執行緒分為前台執行緒和後台執行緒,執行緒建立時不做設定預設是前台執行緒。即執行緒屬性isbackground false。thread.isbackground false false 設定為前台執行緒,系統預設為前台執行緒。區別以及如何使用 這兩者的區別就是 應用程...
SpringBoot 執行緒池(一) 使用同步執行緒池
建立同步任務類synctask,新增 component注釋 為了測試方便,只列印一行資訊 同步任務 public void sync catch interruptedexception e 同步任務執行緒池 private final executorservice executorservic...
執行緒 執行緒池
執行緒池是一種多執行緒處理形式,處理過程中將任務新增到佇列,然後在建立執行緒後執行,主要實現 建立執行緒和管理執行緒,並且給執行緒分配任務。執行緒池中的執行緒是併發執行的。乙個比較簡單的執行緒池至少應包含執行緒池管理器 工作執行緒 任務列隊 任務介面等部分。其中執行緒池管理器的作用是建立 銷毀並管理...