private int i1 = 0, i2 = 0, i3 = 0;
private void button1_Click(object sender, EventArgs e)
{
Task[] tasks = new Task[3];
for (int i = 0; i < 3; i++)
{
tasks[i] = Task.Factory.StartNew(() => testTask(i));
}
Task.WaitAll(tasks);
MessageBox.Show(string.Format("见到你很高兴.{0},{1},{2}", i1, i2, i3));
}
private void setIv(int s, int n)
{
switch (s)
{
case 3:
{
for (int i = 0; i < 10000; i++)
{ }
i1 = n;
break;
}
case 4:
{
for (int i = 0; i < 50000; i++)
{ }
i2 = n;
break;
}
case 5:
{
for (int i = 0; i < 100000; i++)
{ }
i3 = n;
break;
}
}
}
public void testTask(int n)
{
//TODO: Add your code here
switch (n)
{
case 0:
{
setIv(3, 1);
break;
}
case 1:
{
setIv(4, 2);
break;
}
case 2:
{
setIv(5, 3);
break;
}
}
}
public class BookShop
{
Random rand = new Random();
Queue<Book> queueBook = new Queue<Book>();
int Max = 10000;
int EachCount = 500;
int Cur = 0;
void LoadData()
{
if (Cur >= Max)
{
//when Cur>=Max,return
Console.WriteLine("Reached max value!");
return;
}
Console.WriteLine("Loading data...");
for (int i = Cur; i < EachCount + Cur; i++)
{
Thread.Sleep(10);
Console.Write(i + " ");
//insert data into Queue<Book>
queueBook.Enqueue(new Book { Id = i, Name = "Name_" + i });
}
Console.WriteLine("");
Cur += EachCount;
Console.WriteLine("Data loaded successful,{0}...", queueBook.Count);
}
public void Run()
{
LoadData();
int im = EachCount > 10 ? 10 : EachCount;
if (im < 1)
{
return;
}
Console.WriteLine("Tasks have started...");
do
{
Task[] tasks = new Task[im];
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
//// Wait for all tasks to complete.
Task.WaitAll(tasks);
//when Queue<Book> is empty,reload data...
LoadData();
} while (queueBook.Count > 0);
//when Cur>=Max,break the while loop
Console.WriteLine("All tasks have completed.");
}
void DoSomeWork(int val)
{
while (queueBook.Count > 0)
{
var m = queueBook.Dequeue();
if (m == null) continue;
Console.WriteLine("DoSomeWork start,i={0},Current thread id:{1}...", m.Id, Thread.CurrentThread.ManagedThreadId);
var sleep = rand.Next(10, 200);
// Pretend to do something.
Thread.Sleep(sleep);
Console.WriteLine("DoSomeWork has completed,sleep:{0},i={1}", sleep, m.Id);
}
}
}
public class BookShop
{
Random rand = new Random();
Queue<Book> queueBook = new Queue<Book>();
int Max = 10000;
int EachCount = 500;
int Cur = 0;
void LoadData()
{
if (Cur >= Max)
{
//when Cur>=Max,return
Console.WriteLine("Reached max value!");
return;
}
Console.WriteLine("Loading data...");
for (int i = Cur; i < EachCount + Cur; i++)
{
Thread.Sleep(10);
Console.Write(i + " ");
//insert data into Queue<Book>
queueBook.Enqueue(new Book { Id = i, Name = "Name_" + i });
}
Console.WriteLine("");
Cur += EachCount;
Console.WriteLine("Data loaded successful,{0}...", queueBook.Count);
}
public void Run()
{
LoadData();
int im = EachCount > 10 ? 10 : EachCount;
if (im < 1)
{
return;
}
Console.WriteLine("Tasks have started...");
do
{
Task[] tasks = new Task[im];
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
//// Wait for all tasks to complete.
Task.WaitAll(tasks);
//when Queue<Book> is empty,reload data...
LoadData();
} while (queueBook.Count > 0);
//when Cur>=Max,break the while loop
Console.WriteLine("All tasks have completed.");
}
void DoSomeWork(int val)
{
while (queueBook.Count > 0)
{
var m = queueBook.Dequeue();
if (m == null) continue;
Console.WriteLine("DoSomeWork start,i={0},Current thread id:{1}...", m.Id, Thread.CurrentThread.ManagedThreadId);
var sleep = rand.Next(10, 200);
// Pretend to do something.
Thread.Sleep(sleep);
Console.WriteLine("DoSomeWork has completed,sleep:{0},i={1}", sleep, m.Id);
}
}
}
他这个可以终端,是因为他没用用到i,
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
#17
#18
这是奇了怪了!你是写作
for (int i = 0; i < 3; i++)
{
tasks[i] = Task.Factory.StartNew(() => testTask(i));
}
Task.WaitAll(tasks);
public class BookShop
{
Random rand = new Random();
Queue<Book> queueBook = new Queue<Book>();
int Max = 10000;
int EachCount = 500;
int Cur = 0;
void LoadData()
{
if (Cur >= Max)
{
//when Cur>=Max,return
Console.WriteLine("Reached max value!");
return;
}
Console.WriteLine("Loading data...");
for (int i = Cur; i < EachCount + Cur; i++)
{
Thread.Sleep(10);
Console.Write(i + " ");
//insert data into Queue<Book>
queueBook.Enqueue(new Book { Id = i, Name = "Name_" + i });
}
Console.WriteLine("");
Cur += EachCount;
Console.WriteLine("Data loaded successful,{0}...", queueBook.Count);
}
public void Run()
{
LoadData();
int im = EachCount > 10 ? 10 : EachCount;
if (im < 1)
{
return;
}
Console.WriteLine("Tasks have started...");
do
{
Task[] tasks = new Task[im];
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
//// Wait for all tasks to complete.
Task.WaitAll(tasks);
//when Queue<Book> is empty,reload data...
LoadData();
} while (queueBook.Count > 0);
//when Cur>=Max,break the while loop
Console.WriteLine("All tasks have completed.");
}
void DoSomeWork(int val)
{
while (queueBook.Count > 0)
{
var m = queueBook.Dequeue();
if (m == null) continue;
Console.WriteLine("DoSomeWork start,i={0},Current thread id:{1}...", m.Id, Thread.CurrentThread.ManagedThreadId);
var sleep = rand.Next(10, 200);
// Pretend to do something.
Thread.Sleep(sleep);
Console.WriteLine("DoSomeWork has completed,sleep:{0},i={1}", sleep, m.Id);
}
}
}
public class BookShop
{
Random rand = new Random();
Queue<Book> queueBook = new Queue<Book>();
int Max = 10000;
int EachCount = 500;
int Cur = 0;
void LoadData()
{
if (Cur >= Max)
{
//when Cur>=Max,return
Console.WriteLine("Reached max value!");
return;
}
Console.WriteLine("Loading data...");
for (int i = Cur; i < EachCount + Cur; i++)
{
Thread.Sleep(10);
Console.Write(i + " ");
//insert data into Queue<Book>
queueBook.Enqueue(new Book { Id = i, Name = "Name_" + i });
}
Console.WriteLine("");
Cur += EachCount;
Console.WriteLine("Data loaded successful,{0}...", queueBook.Count);
}
public void Run()
{
LoadData();
int im = EachCount > 10 ? 10 : EachCount;
if (im < 1)
{
return;
}
Console.WriteLine("Tasks have started...");
do
{
Task[] tasks = new Task[im];
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
//// Wait for all tasks to complete.
Task.WaitAll(tasks);
//when Queue<Book> is empty,reload data...
LoadData();
} while (queueBook.Count > 0);
//when Cur>=Max,break the while loop
Console.WriteLine("All tasks have completed.");
}
void DoSomeWork(int val)
{
while (queueBook.Count > 0)
{
var m = queueBook.Dequeue();
if (m == null) continue;
Console.WriteLine("DoSomeWork start,i={0},Current thread id:{1}...", m.Id, Thread.CurrentThread.ManagedThreadId);
var sleep = rand.Next(10, 200);
// Pretend to do something.
Thread.Sleep(sleep);
Console.WriteLine("DoSomeWork has completed,sleep:{0},i={1}", sleep, m.Id);
}
}
}
他这个可以终端,是因为他没用用到i,
for (int i = 0; i < im; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSomeWork(1000 * 1));
}
#17
#18
这是奇了怪了!你是写作
for (int i = 0; i < 3; i++)
{
tasks[i] = Task.Factory.StartNew(() => testTask(i));
}
Task.WaitAll(tasks);