c# Thread类线程常用操作详解
作者:UP技术控 发布时间:2021-09-20 21:35:51
标签:c#,Thread,线程
目录
创建线程
管理线程
销毁线程
创建线程
线程是通过扩展 Thread 类创建的。扩展的 Thread 类调用 Start() 方法来开始子线程的执行。
下面的程序演示了这个概念:
class ThreadCreationProgram
{
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
}
static void Main(string[] args)
{
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
}
当上面的代码被编译和执行时,它会产生下列结果:
In Main: Creating the Child thread
Child thread starts
管理线程
Thread 类提供了各种管理线程的方法。
下面的实例演示了 sleep() 方法的使用,用于在一个特定的时间暂停线程。
class ThreadCreationProgram
{
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
// 线程暂停 5000 毫秒
int sleepfor = 5000;
Console.WriteLine("Child Thread Paused for {0} seconds",
sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
static void Main(string[] args)
{
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
}
当上面的代码被编译和执行时,它会产生下列结果:
In Main: Creating the Child thread
Child thread starts
Child Thread Paused for 5 seconds
Child thread resumes
销毁线程
Abort() 方法用于销毁线程。
通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。
下面的程序说明了这点:
class ThreadCreationProgram
{
public static void CallToChildThread()
{
try
{
Console.WriteLine("Child thread starts");
// 计数到 10
for (int counter = 0; counter <= 10; counter++)
{
Thread.Sleep(500);
Console.WriteLine(counter);
}
Console.WriteLine("Child Thread Completed");
}
catch (ThreadAbortException e)
{
Console.WriteLine("Thread Abort Exception");
}
finally
{
Console.WriteLine("Couldn't catch the Thread Exception");
}
}
static void Main(string[] args)
{
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
// 停止主线程一段时间
Thread.Sleep(2000);
// 现在中止子线程
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort();
Console.ReadKey();
}
}
当上面的代码被编译和执行时,它会产生下列结果:
In Main: Creating the Child thread
Child thread starts
0
1
2
In Main: Aborting the Child thread
Thread Abort Exception
Couldn't catch the Thread Exception
来源:https://www.cnblogs.com/lyl6796910/p/14517330.html


猜你喜欢
- 什么是jdkjdk是什么呢?jdk的是java development kit的缩写,意思是java程序开发的工具包。也可以说jdk是jav
- 简介:在团队协作开发的过程中,好的代码管理能更加有效的使日常开发的过程中对各个开发人员提高开发速度。下面将详细介绍在IDEA中使用git提交
- 历史原因当系统启动一个APP时,zygote进程会首先创建一个新的进程去运行这个APP,但是进程的创建是需要时间的,在创建完成之前,界面是呈
- 0x01 新建SpringBoot项目1. 新建maven工程ps:在上一教程的基础上操作,就不用新建项目了,请参考文章:SpringBoo
- 很多小伙伴都有修改Eclipse启动画面或图标的需求,寻找自己的个性化嘛,今天我就给大家说一下如何修改。一、 启动画面自定义第一种情况:.纯
- 1.对包名的判断,异常则说明不存在:try {PackageManager pm = getPackageManager();pm.getP
- 三种定义数组的格式如下:int[] arr1=new int[10];int[] arr2={1,2,3,6};int[] arr3=new
- 1)页面跳转 直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。 返回带有前缀的字符串:转发:
- 熟悉Android的朋友们都知道,不管是微博客户端还是新闻客户端,都离不开列表组件,可以说列表组件是Android数据展现方面最
- --删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk
- JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范. 为Java开发人员提供了一种对象/
- 工程加入依赖:<dependency><groupId>org.apache.pdfbox</groupId&
- 序本文主要研究下迁移到java9的一些注意事项。迁移种类1、代码不模块化,先迁移到jdk9上,好利用jdk9的api2、代码同时也模块化迁移
- 前言大家应该都知道,在Android中,我们对于View进行模拟点击事件,很容易,比如调用View.performClick即可。但是有些时
- 事务处理基本原理 事务是将一系列操作作为一个单元执行,要么成功,要么失败,回滚到
- 排序算法介绍排序也称排序算法(Sort Algorithm),排序是将一组数据,依指定的顺序进行排列的过程。排序的分类:1) 内部排序:指将
- 本文实例为大家分享了java代码统计小程序,供大家参考,具体内容如下可以测试每周你的工作量package rexExp;import jav
- Android实现TextView超链接一共有五种方式:推荐第四种、第五种1. 直接在xml文件中配置autoLink属性(简单易用,效果单
- 本文实例讲述了C#操作ftp类。分享给大家供大家参考。具体如下:using System;using System.Collections.
- 本文实例讲述了java实现统计字符串中字符及子字符串个数的方法。分享给大家供大家参考,具体如下:这里用java实现统计字符串中的字符(包括数