线程练习

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Threading;
 10 using System.Threading.Tasks;
 11 
 12 namespace WindowsFormsApplication1
 13 {
 14     public partial class Form1 : Form
 15     {
 16         public Form1()
 17         {
 18             InitializeComponent();
 19         }
 20 
 21         private void button1_Click(object sender, EventArgs e)
 22         {
 23             Thread t = new Thread(test1);
 24             t.Start();
 25 
 26             Thread t1 = new Thread(new ThreadStart(test1));
 27             t1.Start();
 28 
 29 
 30             Thread t2 = new Thread(() => { test1(); });
 31             t2.Start();
 32 
 33             Thread t3 = new Thread(delegate() { test1(); });
 34             t3.Start();
 35 
 36             Thread t4 = new Thread(() => { Console.WriteLine("test1" + Thread.CurrentThread.ManagedThreadId); });
 37             t4.Start();
 38 
 39 
 40             Task task = new Task(test1);
 41             task.Start();
 42 
 43 
 44             Thread t5 = new Thread(test2);
 45             t5.Start(2);
 46 
 47             Thread t6 = new Thread(() => { test2(2); });
 48             t5.Start();
 49 
 50             Thread t7 = new Thread((o => { test2(o); }));
 51             t7.Start(3);
 52 
 53             Thread t8 = new Thread(new ParameterizedThreadStart(test2));//参数类型必须object 且只有一个参数
 54             t8.Start(2);
 55 
 56             Test4 test = new Test4();
 57             test.a = 4;
 58             Thread t9 = new Thread(test.test4);
 59             t9.Start();
 60 
 61             ThreadPool.QueueUserWorkItem(o => { test1(); });
 62             Parallel.Invoke
 63                 (
 64                 () => { test1(); }
 65                 );
 66 
 67             var action = new Action[]
 68             {
 69                 ()=>test1(),
 70             };
 71             Parallel.Invoke(action);
 72 
 73             Task.Factory.StartNew(() => { });
 74 
 75 
 76 
 77             TaskFactory taskFactory = new TaskFactory();
 78             List<Task> taskList = new List<Task>();
 79 
 80             Task any = taskFactory.ContinueWhenAny(taskList.ToArray(), t =>
 81             {
 82 
 83                 //t.AsyncState
 84 
 85                 Console.WriteLine("这里是ContinueWhenAny {0}", Thread.CurrentThread.ManagedThreadId);
 86 
 87             });
 88 
 89 
 90 
 91             Task all = taskFactory.ContinueWhenAll(taskList.ToArray(), tList =>
 92             {
 93 
 94                 Console.WriteLine("这里是ContinueWhenAll {0}", Thread.CurrentThread.ManagedThreadId);
 95 
 96             });
 97 
 98 
 99 
100 
101 
102             Task.WaitAny(taskList.ToArray());//执行的线程等待某一个task的完成
103 
104             Console.WriteLine("after WaitAny{0}", Thread.CurrentThread.ManagedThreadId);
105 
106             Task.WaitAll(taskList.ToArray());//执行的线程等待全部的task的完成
107 
108             Console.WriteLine("after WaitAll{0}", Thread.CurrentThread.ManagedThreadId);
109         }
110 
111 
112 
113         private void test1()
114         {
115             Console.WriteLine("test1" + Thread.CurrentThread.ManagedThreadId);
116         }
117 
118         private void test2(object a)
119         {
120             Console.WriteLine("test2" + Thread.CurrentThread.ManagedThreadId);
121 
122         }
123 
124         private int test3(int a)
125         {
126             Console.WriteLine("test3" + Thread.CurrentThread.ManagedThreadId);
127             return 3;
128         }
129     }
130 
131     public class Test4
132     {
133         public int a { get; set; }
134 
135         public void test4()
136         {
137             Console.WriteLine("test" + a + Thread.CurrentThread.ManagedThreadId);
138         }
139     }
140 }
原文地址:https://www.cnblogs.com/anyihen/p/12826350.html