首页| JavaScript| HTML/CSS| Matlab| PHP| Python| Java| C/C++/VC++| C#| ASP| 其他|
购买积分 购买会员 激活码充值

您现在的位置是:虫虫源码 > C# > c# 多线程操作例子源码

c# 多线程操作例子源码

  • 资源大小:0.08M
  • 上传时间:2021-07-20
  • 下载次数:0次
  • 浏览次数:0次
  • 资源积分:1积分
  • 标      签: Windows系统编程

资 源 简 介

多线程操作 [实例截图] [核心代码]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 ThreadMore{ public partial class Form3 : Form { public Form3() { InitializeComponent(); } // 发送数据包的类 public class SendPackage { Package pack; // 保存将被发送的数据包 int quantity = 1; // 数据包的个数 // 发送数据包 public SendPackage(Package p, int count) { pack = p; quantity = count; } public void ThreadRun() { // 发送指定数量的数据包 for (int looper = 1; looper <= quantity; looper ) pack.Send(looper); // 调用数据包对象的发送方法 } } // 接收数据包的类 public class ReceivePackage { Package pack; int quantity = 1; // 接收数据包 public ReceivePackage(Package p, int count) { pack = p; quantity = count; } public void ThreadRun() { int valReturned; // 接收指定数量的数据包 for (int looper = 1; looper <= quantity; looper ) valReturned = pack.Receive(); } } // 数据包类********************************************************************** public class Package { int packContents;// 数据包的内容 bool readerFlag = false;// 状态标识,初始值设置为false,表示只能发送而不能接收 // 接收方法 public int Receive() { //Monitor.Enter(this); // 获取排他锁 lock (this)//等同于Monitor.Enter(this);Monitor.Exit(this); { if (!readerFlag) {// 等待数据包到达 try { Monitor.Wait(this); // 释放对象上的锁并阻止当前线程,直到它重新获取该锁 } catch (SynchronizationLockException e) { Console.WriteLine(e); } catch (ThreadInterruptedException e) { Console.WriteLine(e); } } //Console.WriteLine("...接收({0,2})", packContents); Console.WriteLine("...接收:{0}", packContents); readerFlag = false; // 表示接收结束 Monitor.Pulse(this); // 通知其他线程 } //Monitor.Exit(this); // 释放排他锁 return packContents; } // 发送数据 public void Send(int n) { //Monitor.Enter(this); lock (this) { if (readerFlag) {// 等待前一个数据包被接收 try { Monitor.Wait(this); } catch (SynchronizationLockException e) { Console.WriteLine(e); } catch (ThreadInterruptedException e) { Console.WriteLine(e); } } packContents = n; //Console.Write("发送({0,2})...", packContents); Console.Write("发送:{0}...", packContents); readerFlag = true; Monitor.Pulse(this); } //Monitor.Exit(this); } } //********************************************************************************** //银行交易类 private class Account { int balance; Random r = new Random(); public Account(int initial) { balance = initial; } //取钱 int Withdraw(int amount) { if (balance < 0) { throw new Exception("余额为负!"); } lock (this) { if (balance >= amount) { Console.WriteLine("原有余额:" balance); Console.WriteLine("支取余额:-" amount); balance = balance - amount; Console.WriteLine("现有余额:" balance); return amount; } else { return 0;//拒绝交易 } } } //测试交易 public void DoTransactions() { //支取随机的金额100次 for (int i = 0; i < 100; i ) { Withdraw(r.Next(1, 100)); } } } //********************************************************************************** private void button1_Click(object sender, EventArgs e) { Package pack = new Package(); // 创建包含线程方法的对象 SendPackage server = new SendPackage(pack, 20); ReceivePackage client = new ReceivePackage(pack, 20); // 创建两个线程对象,一个用于发送数据包,另一个用于接收数据包 Thread producer = new Thread(new ThreadStart(server.ThreadRun)); Thread consumer = new Thread(new ThreadStart(client.ThreadRun)); try { // 启动两个线程 producer.Start(); consumer.Start(); // 等待两个线程结束 producer.Join(); consumer.Join(); } catch (ThreadStateException ex) { Console.WriteLine(e); } catch (ThreadInterruptedException ex) { Console.WriteLine(e); } } private void button2_Click(object sender, EventArgs e) { //建立10个线程同时进行交易 Thread[] threads = new Thread[10]; Account acc = new Account(1000); for (int i = 0; i < 10; i ) { threads[i] = new Thread(new ThreadStart(acc.DoTransactions)); } for (int i = 0; i < 10; i ) { threads[i].Start(); } } }}
VIP VIP
0.181251s