C#多线程的简单例子
2024-10-17 04:55:07
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
//using System.Threading.Tasks;
using System.IO;
using System.Collections; namespace ConsoleApplication2
{ class program
{
static void Main(string[] args)
{
Console.WriteLine("开始一个新的线程,名为次线程");
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
for (int i = ; i < ; i++)
{
Console.WriteLine("主线程:" + i);
Thread.Sleep();
}
Console.WriteLine("调用Join函数等待次线程结束");
//当次线程执行完毕后,Join阻塞调用线程,直到某个线程终止为止,本例为次线程
t.Join();
Console.WriteLine("线程执行完毕");
}
public static void ThreadProc()
{
for (int i = ; i < ; i++)
{
Console.WriteLine("ThreadPorc:{0}", i);
Thread.Sleep();//将当前进程阻塞指定的毫秒数
}
} }
}
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(Thread1));
Thread t2 = new Thread(new ThreadStart(Thread2)); t1.Priority = ThreadPriority.BelowNormal ;
t2.Priority = ThreadPriority.Lowest ;
t1.Start();
t2.Start();
}
public static void Thread1()
{
for (int i = ; i < ; i++)
{//每运行一个循环就写一个“1”
dosth();
Console.Write("");
}
}
public static void Thread2()
{
for (int i = ; i < ; i++)
{//每运行一个循环就写一个“2”
dosth();
Console.Write("");
}
}
public static void dosth()
{//用来模拟复杂运算
for (int j = ; j < ; j++)
{
int a=;
a = a*a*a*a;
}
}
最新文章
- sublime text 3 3083 注册码
- 数据库SQL优化大总结之 百万级数据库优化方案
- iOS 判断字符串是否为空
- 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法
- 【2016-10-11】Linux系统常用的关机或重启命令shutdown、reboot、halt、poweroff、init 0及init 6的联系与区别
- Android Activity的加载模式和onActivityResult方法之间的冲突
- 0414-复利计算器6.0.Release
- ASP.NET MVC4中使用Ninject
- awk 传入外部参数
- Mysql 批量建表存储过程
- root 密码丢失后的重新设置
- 为什么我选择使用 Blocks(块)
- JQ 日期格式化
- 在线程中进行读取并写入文件和wenjia
- asp net 编程问题 实现下一篇 和上一篇效果
- 架构漫谈系列(2) 封装(Encapsulation)
- poj1067威佐夫博奕
- JAVA 平时作业一
- 从原型链探究Javascript这么火的原因
- TCP/IP协议(一)网络基础知识 网络七层协议
热门文章
- tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
- 每天一个linux命令(15):head命令
- js中call apply方法的使用介绍
- Composer + thinkphp5.1安装与使用
- jQuery 实现最简单的form表单提交 Loding 功能
- @property_@synthesize 配套使用
- maven配置的问题,maven的环境变量配置
- iOS开发之---KVC全解
- C Primer Plus学习笔记(六)- C 控制语句:分支和跳转
- DataGridView风格设置