步步为营-64-进程&线程
1 进程
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 进程线程
{
class Program
{
static void Main(string[] args)
{
//获取当前进程
Process p1 = Process.GetCurrentProcess();
Console.WriteLine(p1.Id);
Console.WriteLine(p1.ProcessName); //打开新的进程
Process p2 = Process.Start("cmd.exe");
string key = Console.ReadLine();
if (key=="k")
{
//杀死进程
p2.Kill();
}
Console.ReadLine(); }
}
}

2 应用程序域
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 应用程序域
{
class Program
{
static void Main(string[] args)
{
//01 获取当前应用程序域
AppDomain ad = AppDomain.CurrentDomain;
Console.WriteLine(ad.FriendlyName);
//02 在当前程序域中打开程序集,不会开启新进程
AppDomain ap2= AppDomain.CreateDomain("xyxtl");
int id = ap2.ExecuteAssembly("进程线程.exe");
Console.Write(id); Console.ReadKey();
}
}
}

3 线程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 线程
{
class Program
{
static void Main(string[] args)
{
#region 01 获取当前线程-默认程序启动后,会有一个主线程
Thread t1 = Thread.CurrentThread;
Console.WriteLine(t1.ManagedThreadId);
#endregion #region 02 开辟一个新线程 - 02-01 无参;02-02 有参
//02-01 有参
Thread t2 = new Thread(() =>
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);//输出当前线程编号
Console.WriteLine("无参,构造函数!");
});
t2.Start(); //02-02 有参
#endregion Thread t3 = new Thread((p) =>
{
//由于参数是object类型,如果想访问对象特有成员,需要进行类型转换
Person p2 = p as Person;
if (p2!=null)
{
;
Console.WriteLine(p2.Age);
} Console.WriteLine(p.ToString());
});
t3.Start(new Person()
{
Name = "张三",
Age = ,
});
Console.Read();
} public class Person
{
public string Name { get; set; }
public int Age { get; set; } public override string ToString()
{
return string.Format("姓名:{0},年龄{1}",Name,Age);
}
}
}
}

3.2 IsBackground属性
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 前台线程与后台线程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(() =>
{
while (true)
{
Console.WriteLine();
}
});
//休息5秒
// Thread.Sleep(5000); t1.Start();
} private void button2_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(() =>
{
while (true)
{
Console.WriteLine();
}
});
t1.IsBackground = true;
t1.Start();
}
}
}
当线程是后台线程时,主线程关闭,后台线程也随之关闭;
当线程是前台线程时,主线程关闭,前台线程不关闭;
3.3 join 属性,阻塞join代码所在的当前线程==插队


4 lock
问题的引出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Lock锁
{
class Program
{
static void Main(string[] args)
{
int num = ; Thread th = new Thread(() =>
{
for (int i = ; i < ; i++)
{
num--;
}
});
th.IsBackground = true;
th.Start(); for (int i = ; i < ; i++)
{
num++;
} Console.WriteLine(num);
Console.ReadKey();
}
}
}

解决方法:加锁
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Lock锁
{
class Program
{
static void Main(string[] args)
{
int num = ; Thread th = new Thread(() =>
{
for (int i = ; i < ; i++)
{
lock ("B")
{
num --;
}
}
});
th.IsBackground = true;
th.Start(); for (int i = ; i < ; i++)
{
lock ("B")
{
num ++;
}
} Console.WriteLine(num);
Console.ReadKey();
}
}
}

5 栈 stack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 栈
{
class Program
{
static void Main(string[] args)
{
//定义栈
Stack<BaoZi> bzStack = new Stack<BaoZi>( );
//入栈
bzStack.Push(new BaoZi());
//出栈
if (bzStack.Count>)
{
BaoZi bz= bzStack.Pop();
} } public class BaoZi
{
}
}
}
6 线程池
线程池中的线程都是后台线程
不能手动设置每个线程的属性(前台,优先级)
比较短的任务考虑线程池,比较长的任务考虑手动创建一个线程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 线程池
{
class Program
{
static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
ThreadPool.QueueUserWorkItem((obj) =>
{
Console.WriteLine(obj+"_"+Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
},i
);
}
Console.Read();
}
}
}

7 异步方式调用委托对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 异步方式调用委托对象
{
class Program
{
static void Main(string[] args)
{
//定义一个委托
Action<string> s1 = (s) =>
{
Console.WriteLine(s+"_"+ Thread.CurrentThread.ManagedThreadId);
};
//委托的实现-01
//s1("张三");
//委托的实现-02 异步调用实现委托
s1.BeginInvoke("张三",Func1,""); //获取主线程id
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); Console.ReadKey();
} #region 委托实现后的回调函数
private static void Func1(IAsyncResult ar)
{
Console.WriteLine("李四"+"_"+Thread.CurrentThread.ManagedThreadId);
}
#endregion
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace 异步方式调用委托对象
{
class Program
{
static void Main(string[] args)
{
//定义一个委托
Action<string> s1 = (s) =>
{
Console.WriteLine(s+"_"+ Thread.CurrentThread.ManagedThreadId);
};
//委托的实现-01
//s1("张三");
//委托的实现-02 异步调用实现委托
IAsyncResult result = s1.BeginInvoke("张三",Func1,"");
//保证委托执行完成后,执行后续代码
//只保证委托执行完成,不保证回调函数也执行完成
s1.EndInvoke(result); //获取主线程id
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); Console.ReadKey();
} #region 委托实现后的回调函数
private static void Func1(IAsyncResult ar)
{
Console.WriteLine("李四"+"_"+Thread.CurrentThread.ManagedThreadId);
}
#endregion
}
}

8 并行计算
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 并行计算
{
class Program
{
static void Main(string[] args)
{
Stopwatch sp = new Stopwatch();
sp.Start();
//普通计算
//for (int i = 0; i < 100000; i++)
//{
// Console.WriteLine(i);
//}
//并行计算
Parallel.For(1, 100000, (i) => { Console.WriteLine(i); });
sp.Stop();
Console.WriteLine(sp.Elapsed);
Console.ReadKey();
}
}
}

步步为营-64-进程&线程的更多相关文章
- Linux查看进程线程个数
1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...
- python基础(16)-进程&线程&协程
进程之multiprocessing模块 Process(进程) Process模块是一个创建进程的模块,借助这个模块,就可以完成进程的创建. 介绍 初始化参数 Process([group [, t ...
- [5]windows内核情景分析---进程线程
本篇主要讲述进程的启动过程.线程的调度与切换.进程挂靠 进程的启动过程: BOOL CreateProcess ( LPCTSTR lpApplicationName, ...
- XV6源代码阅读-进程线程
Exercise1 源代码阅读 1.基本头文件:types.h param.h memlayout.h defs.h x86.h asm.h mmu.h elf.h types.h:仅仅是定义uint ...
- python学习笔记-进程线程
1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...
- 获取系统中所有进程&线程信息
读书笔记--[计算机病毒解密与对抗] 目录: 遍历进程&线程程序 终止进程 获取进程信息 获取进程内模块信息 获取进程命令行参数 代码运行环境:Win7 x64 VS2012 Update3 ...
- [skill] 进程 线程
在业务逻辑上: 进程线程没有区别. 在系统资源上: 进程拥有自己的地址空间.线程拥有自己的堆栈和临时变量,与其他线程共享地址空间. 在通信代价上: 线程间通信代价更低,实现更方便.进程通信相对开销比较 ...
- pyhon——进程线程、与协程基础概述
一直以来写博客都是实用主义者,只写用法,没信心写原理,但是每一次写作业的过程都有一种掘地三尺的感觉,终于,写博客困难症重症患者经历了漫长的思想斗争,还是决定把从网上淘到的各种杂货和自己的总结放在一起, ...
- android 进程/线程管理(一)----消息机制的框架
一:android 进程和线程 进程是程序运行的一个实例.android通过4大主件,弱化了进程的概念,尤其是在app层面,基本不需要关系进程间的通信等问题. 但是程序的本质没有变,尤其是多任务系统, ...
- android 进程/线程管理(二)----关于线程的迷思
一:进程和线程的由来 进程是计算机科技发展的过程的产物. 最早计算机发明出来,是为了解决数学计算而发明的.每解决一个问题,就要打纸带,也就是打点. 后来人们发现可以批量的设置命令,由计算机读取这些命令 ...
随机推荐
- 函数和常用模块【day06】:shelve模块(五)
本节内容 1.简述 2.shelve概念 3.shelve模块使用 4.总结 一.简述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,不能dum ...
- Linux命令之如何从普通用户切换至管理员用户
普通用户,标志是一个$符号 管理员用户,标志是一个#符号 我要切换,敲打命令 sudo su - 然后输入你的管理员用户的密码(输入密码的时候是不可见的) 然后你就切换到#状态了.
- IoC之Spring.Net在Mvc项目中的使用
MVC中使用Spring.net 前面学习了使用Autofac来实现控制反转,这里简单记录Spring.Net实现IoC和DI的步骤 第一步:安装如下Nuget包 (Spring.Web.Mvc) i ...
- JS控制CSS3,添加浏览器兼容前缀
不同浏览器对于有些css3属性名字定义的时候,会带上特有的前缀,所以在css编写的时候,经常会一个属性写多个不同的前缀进行兼容.比如: div { transform: rotate(30deg); ...
- Dubbo集群容错
转自dubbo官网文档http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在 ...
- CodeChef - CRYPCUR
题目链接 AMRExchange is the latest cryptocurrency exchange that has become very popular among cryptocurr ...
- 使用JS监听键盘按下事件(keydown event)
1.监听全局键盘按下事件,例如监听全局回车事件 1 $(document).keydown(function(event){ 2 if(event.keyCode == 13){ 3 alert('你 ...
- python - 远程主机执行命令练习(socket UDP + subprocess.Popen()) 练习1
环境是windows 环境. server端: import socket import subprocess ss = socket.socket(socket.AF_INET,socket.SOC ...
- yum和rpm的区别
rpm是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装.查询.卸载.升级等工作.但是rpm软件包之间的依赖性问题往往会很繁琐,尤其是软件由多个rpm包组成时.Yum(全称为 Y ...
- SpringMVC使用Hession发布远程服务
(1)三个项目,Api(存放提供者和消费者共有的xx,例如实体类以及服务接口等等).Service(服务提供者).Provider(服务消费者) Api部分代码 package cn.coreqi.e ...