码迷,mamicode.com
首页 > 编程语言 > 详细

线程练习

时间:2020-05-04 15:52:09      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:partial   ram   pac   item   init   nbsp   param   work   sap   

  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 
 78         private void test1()
 79         {
 80             Console.WriteLine("test1" + Thread.CurrentThread.ManagedThreadId);
 81         }
 82 
 83         private void test2(object a)
 84         {
 85             Console.WriteLine("test2" + Thread.CurrentThread.ManagedThreadId);
 86 
 87         }
 88 
 89         private int test3(int a)
 90         {
 91             Console.WriteLine("test3" + Thread.CurrentThread.ManagedThreadId);
 92             return 3;
 93         }
 94     }
 95 
 96     public class Test4
 97     {
 98         public int a { get; set; }
 99 
100         public void test4()
101         {
102             Console.WriteLine("test" + a + Thread.CurrentThread.ManagedThreadId);
103         }
104     }
105 }

 

线程练习

标签:partial   ram   pac   item   init   nbsp   param   work   sap   

原文地址:https://www.cnblogs.com/anyihen/p/12826350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!