zoukankan      html  css  js  c++  java
  • 多线程笔记

    (一)线程同步
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;

    namespace _03_多线程
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
                //无法在一个线程中去操作另一个线程创建的控件
                Control.CheckForIllegalCrossThreadCalls = false;

                //定义了一个线程
                Thread th = new Thread(new ThreadStart(Test));
                th.Name = "th0";
                th.Start();

                Thread th1 = new Thread(Test);
                th1.Name = "th1";
                th1.Start();
            }

            object o = new object();
            //开辟内存空间
            //调用构造函数
            //存储同步块索引(默认负数)
            void Test()
            {
                for (int i = 0; i < 1000; i++)
                {
                    //线程同步
                    lock(o)
                    {
                        int num = int.Parse(textBox1.Text);
                        num++;
                        textBox1.Text = num.ToString();
                        Console.WriteLine(Thread.CurrentThread.Name + "==" + num);
                    }
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Test");
            }
        }
    }
    (二)线程同步
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using System.Threading;
    namespace _04_前后台线程
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(Test);
                //后台线程
                //th.IsBackground = true;
                th.Start();

                MessageBox.Show("Test111");
            }

            void Test()
            {
                for (int i = 0; i < 100000; i++)
                {
                    Console.WriteLine(i);
                }
            }

            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Test");
            }
        }
    }

    (三)摇奖机
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using System.Threading;
    namespace _05_摇奖机
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //不检查跨线程操作的合法性
                Control.CheckForIllegalCrossThreadCalls = false;
            }

            //默认没有开始
            bool isStart = false;
            Thread th;
            private void button1_Click(object sender, EventArgs e)
            {
                //开始动态显示数字
                if (!isStart)
                {
                    th = new Thread(Test);
                    th.IsBackground = true;
                    th.Start();

                    isStart = true;
                    button1.Text = "停止";
                }
                //停止动态显示数字
                else
                {
                    isStart = false;
                    button1.Text = "开始";
                    th.Abort();
                }
            }
            void Test()
            {
                Random r = new Random();
                while (true)
                {
                    //label中显示随机数字
                    System.Threading.Thread.Sleep(100);
                    label1.Text = r.Next(0, 10).ToString();
                }
            }
        }
    }

    (四)带参数的多线程
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    using System.Threading;
    namespace _06_执行带参数的方法
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string[] names = { "蒋卫生","尤期间","马户"};

                Thread th = new Thread(Test);
                th.Start(names);
            }


            void Test(object o)
            {
                string[] arr = o as string[];
                foreach (string item in arr)
                {
                    MessageBox.Show(item);
                }
            }
        }
    }

  • 相关阅读:
    Leetcode 421.数组中两数的最大异或值
    Leetcode 419.甲板上的战舰
    Leetcode 417.太平洋大西洋水流问题
    Leetcode 416.分割等和子集
    Leetcode 413.等差数列划分
    Leetcode 410.分割数组的最大值
    由股票价格形成因素看如何选股(发布于06-02 13:09)
    巴菲特的资产配置艺术(发布于06-01 13:28)
    ROE能否包打天下?(发布于05-31 11:27)
    价值迟迟不回归怎么办?(发布于05-30 10:02)
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2640587.html
Copyright © 2011-2022 走看看