using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;namespace ConsoleApplication5 { class Program { static void Main(string[] args) { Console.WriteLine(value); Thread[] t = new Thread[10]; for (int i = 0 ; i < 10 ; i++) { ares[i] = new AutoResetEvent(false); t[i] = new Thread(M); t[i].Start(i); } WaitHandle.WaitAll(ares); Console.WriteLine(value); Console.ReadKey(); } static AutoResetEvent[] ares = new AutoResetEvent[10]; static object o = new object(); static int value = 0; static void M(object i) { lock (o) { while (value !=100) { value++; } ares[int.Parse(i.ToString())].Set(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;namespace ConsoleApplication5 { class Program { static void Main(string[] args) { Console.WriteLine(value); Thread[] t = new Thread[10]; for (int i = 0 ; i < 10 ; i++) { ares[i] = new AutoResetEvent(false); t[i] = new Thread(M); t[i].Start(i); } WaitHandle.WaitAll(ares); Console.WriteLine(value); Console.ReadKey(); } static AutoResetEvent[] ares = new AutoResetEvent[10]; static object o = new object(); static int value = 1; static void M(object i) { lock (o) { while (value % 10 != 0) { value++; } if (value != 100) value++; ares[int.Parse(i.ToString())].Set(); } } } }
private void button1_Click(object sender, EventArgs e) { Thread[] t= new Thread[10]; int iTotal = 0; for (int i = 0; i < t.Length; i++) { t[i] = new Thread(new ParameterizedThreadStart(delegate(object o) { int k = (int)o * 10+1; for (int j = k; j < k + 10; j++) iTotal += j; })); t[i].Start(i); t[i].Join(); } MessageBox.Show(iTotal.ToString()); }
12楼把start和join写在一起,所以不会十个线程同时执行吧...
不会同时执行,可以把Join移出来,另外写一个循环执行行Join
C#4 用,System.Threading.Tasks.Task应该更简单 代码参考:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConsoleApplication7 { class Program { static void Main(string[] args) { Task[] ts = new Task[10]; int value = 0; ts[0] = new Task(() => { for (int i = 0 ; i < 10 ; i++) { value++; } }); ts[0].Start(); for (int i = 0 ; i < 10 - 1 ; i++) { //ts[i+1]=new Task( ts[i + 1] = ts[i].ContinueWith(pt => { if (value < 100) for (int j = 0 ; j < 10 ; j++) { value++; } }); } Task.WaitAll(ts); Console.WriteLine(value); Console.ReadKey(); } } }
不要自己创建线程了,有现成的库支持 namespace ConsoleApplication3 { sealed class CalculateSeg { public int start, end; public CalculateSeg(int _start, int _end) { start = _start; end = _end; } } class Program { static void Main(string[] args) { for (int i = 0; i < 10; ++i) { System.ComponentModel.BackgroundWorker bkThread = new System.ComponentModel.BackgroundWorker(); bkThread.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(bkThread_RunWorkerCompleted); bkThread.DoWork += new System.ComponentModel.DoWorkEventHandler(bkThread_DoWork); bkThread.RunWorkerAsync(new CalculateSeg(i * 10 + 1, i * 10 + 10)); } Console.ReadKey(); } static void bkThread_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { CalculateSeg seg = e.Argument as CalculateSeg; int ret = 0; for (int i = seg.start; i <= seg.end; ++i) { Console.WriteLine(i); ret += i; } e.Result = ret; } static int sum = 0; static void bkThread_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { sum += (int)e.Result; Console.WriteLine("sum={0} ", sum); } } }
private int i = 0; private int x = 0; public Form1() { Thread t1 = new Thread(T1); Thread t2 = new Thread(T2); t1.Start(); t2.Start(); } public void T1() { while ( i <=100) { x += i++; } MessageBox.Show(x.ToString()); } public void T2() { while (i<=100) { x += i++; } MessageBox.Show(x.ToString()); }
没怎么优化了,基本上是这样做的 调用 for (int i = 0; i < 10; i++) { MyThread thread = new MyThread("Thread" + i); thread.Start(); } —————————————— using System; using System.Collections.Generic; using System.Text; using System.Threading;namespace ConsoleApplication1 { class MyThread { private static int num = 1; private static int count = 0; private static object _lockObj = new object(); private Thread _thread = null; private string _name = string.Empty; public MyThread(string name) { _name = name; if (_thread == null) _thread = new Thread(new ThreadStart(ThreadMethod)); } public void Start() { if (null != _thread) _thread.Start(); } private void ThreadMethod() { while (true) { lock (_lockObj) { //Console.WriteLine(_name); if (num < 100) { count += num; num += 1; } if (num == 100) { Console.WriteLine("1到100和是:" + count); break; } } } if (null != _thread) { //Console.WriteLine("线程已关闭"); _thread.Abort(); } } } }
http://topic.csdn.net/u/20100504/10/247c7dbf-3d0a-4b6d-aaa9-da7327804667.html?15972
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ConsoleApplication5 {
class Program {
static void Main(string[] args) {
Console.WriteLine(value);
Thread[] t = new Thread[10];
for (int i = 0 ; i < 10 ; i++) {
ares[i] = new AutoResetEvent(false);
t[i] = new Thread(M);
t[i].Start(i);
}
WaitHandle.WaitAll(ares);
Console.WriteLine(value);
Console.ReadKey();
}
static AutoResetEvent[] ares = new AutoResetEvent[10];
static object o = new object();
static int value = 0;
static void M(object i) {
lock (o) {
while (value !=100) {
value++;
}
ares[int.Parse(i.ToString())].Set();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ConsoleApplication5 {
class Program {
static void Main(string[] args) {
Console.WriteLine(value);
Thread[] t = new Thread[10];
for (int i = 0 ; i < 10 ; i++) {
ares[i] = new AutoResetEvent(false);
t[i] = new Thread(M);
t[i].Start(i);
}
WaitHandle.WaitAll(ares);
Console.WriteLine(value);
Console.ReadKey();
}
static AutoResetEvent[] ares = new AutoResetEvent[10];
static object o = new object();
static int value = 1;
static void M(object i) {
lock (o) {
while (value % 10 != 0) {
value++;
}
if (value != 100)
value++;
ares[int.Parse(i.ToString())].Set();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{ Thread[] t= new Thread[10];
int iTotal = 0;
for (int i = 0; i < t.Length; i++)
{
t[i] = new Thread(new ParameterizedThreadStart(delegate(object o)
{
int k = (int)o * 10+1;
for (int j = k; j < k + 10; j++)
iTotal += j;
}));
t[i].Start(i);
t[i].Join();
} MessageBox.Show(iTotal.ToString());
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication7 {
class Program {
static void Main(string[] args) {
Task[] ts = new Task[10];
int value = 0;
ts[0] = new Task(() => {
for (int i = 0 ; i < 10 ; i++) {
value++;
}
});
ts[0].Start();
for (int i = 0 ; i < 10 - 1 ; i++) {
//ts[i+1]=new Task(
ts[i + 1] = ts[i].ContinueWith(pt => {
if (value < 100)
for (int j = 0 ; j < 10 ; j++) {
value++;
}
});
}
Task.WaitAll(ts);
Console.WriteLine(value);
Console.ReadKey();
}
}
}
namespace ConsoleApplication3
{
sealed class CalculateSeg
{
public int start, end;
public CalculateSeg(int _start, int _end)
{
start = _start;
end = _end;
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; ++i)
{
System.ComponentModel.BackgroundWorker bkThread = new System.ComponentModel.BackgroundWorker();
bkThread.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(bkThread_RunWorkerCompleted);
bkThread.DoWork += new System.ComponentModel.DoWorkEventHandler(bkThread_DoWork);
bkThread.RunWorkerAsync(new CalculateSeg(i * 10 + 1, i * 10 + 10));
}
Console.ReadKey();
} static void bkThread_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
CalculateSeg seg = e.Argument as CalculateSeg;
int ret = 0;
for (int i = seg.start; i <= seg.end; ++i)
{
Console.WriteLine(i);
ret += i;
}
e.Result = ret;
}
static int sum = 0;
static void bkThread_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
sum += (int)e.Result;
Console.WriteLine("sum={0} ", sum);
}
}
}
private int x = 0; public Form1()
{ Thread t1 = new Thread(T1); Thread t2 = new Thread(T2); t1.Start();
t2.Start();
} public void T1()
{
while ( i <=100)
{
x += i++;
}
MessageBox.Show(x.ToString());
} public void T2()
{
while (i<=100)
{
x += i++;
}
MessageBox.Show(x.ToString());
}
调用
for (int i = 0; i < 10; i++)
{
MyThread thread = new MyThread("Thread" + i);
thread.Start();
}
——————————————
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
class MyThread
{
private static int num = 1;
private static int count = 0;
private static object _lockObj = new object();
private Thread _thread = null;
private string _name = string.Empty; public MyThread(string name)
{
_name = name;
if (_thread == null)
_thread = new Thread(new ThreadStart(ThreadMethod));
} public void Start()
{
if (null != _thread)
_thread.Start(); } private void ThreadMethod()
{
while (true)
{
lock (_lockObj)
{
//Console.WriteLine(_name);
if (num < 100)
{
count += num;
num += 1;
}
if (num == 100)
{
Console.WriteLine("1到100和是:" + count);
break;
}
} }
if (null != _thread)
{
//Console.WriteLine("线程已关闭");
_thread.Abort();
}
}
}
}