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-进程&线程的更多相关文章

  1. Linux查看进程线程个数

    1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...

  2. python基础(16)-进程&线程&协程

    进程之multiprocessing模块 Process(进程) Process模块是一个创建进程的模块,借助这个模块,就可以完成进程的创建. 介绍 初始化参数 Process([group [, t ...

  3. [5]windows内核情景分析---进程线程

    本篇主要讲述进程的启动过程.线程的调度与切换.进程挂靠 进程的启动过程: BOOL CreateProcess ( LPCTSTR lpApplicationName,                 ...

  4. XV6源代码阅读-进程线程

    Exercise1 源代码阅读 1.基本头文件:types.h param.h memlayout.h defs.h x86.h asm.h mmu.h elf.h types.h:仅仅是定义uint ...

  5. python学习笔记-进程线程

    1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...

  6. 获取系统中所有进程&线程信息

    读书笔记--[计算机病毒解密与对抗] 目录: 遍历进程&线程程序 终止进程 获取进程信息 获取进程内模块信息 获取进程命令行参数 代码运行环境:Win7 x64 VS2012 Update3 ...

  7. [skill] 进程 线程

    在业务逻辑上: 进程线程没有区别. 在系统资源上: 进程拥有自己的地址空间.线程拥有自己的堆栈和临时变量,与其他线程共享地址空间. 在通信代价上: 线程间通信代价更低,实现更方便.进程通信相对开销比较 ...

  8. pyhon——进程线程、与协程基础概述

    一直以来写博客都是实用主义者,只写用法,没信心写原理,但是每一次写作业的过程都有一种掘地三尺的感觉,终于,写博客困难症重症患者经历了漫长的思想斗争,还是决定把从网上淘到的各种杂货和自己的总结放在一起, ...

  9. android 进程/线程管理(一)----消息机制的框架

    一:android 进程和线程 进程是程序运行的一个实例.android通过4大主件,弱化了进程的概念,尤其是在app层面,基本不需要关系进程间的通信等问题. 但是程序的本质没有变,尤其是多任务系统, ...

  10. android 进程/线程管理(二)----关于线程的迷思

    一:进程和线程的由来 进程是计算机科技发展的过程的产物. 最早计算机发明出来,是为了解决数学计算而发明的.每解决一个问题,就要打纸带,也就是打点. 后来人们发现可以批量的设置命令,由计算机读取这些命令 ...

随机推荐

  1. MyBatis中resultType和resultMap的区别

    resultType和resultMap功能类似  ,都是返回对象信息  ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...

  2. Neural Networks and Deep Learning 课程笔记(第二周)神经网络的编程基础 (Basics of Neural Network programming)

    总结 一.处理数据 1.1 向量化(vectorization) (height, width, 3) ===> 展开shape为(heigh*width*3, m)的向量 1.2 特征归一化( ...

  3. 解决audio和video在手机端无法自动播放问题

    各大浏览器都为了节省流量,做出了优化,在用户没有行为动作时(交互)不予许自动播放 <audio src="music/bg.mp3" autoplay loop contro ...

  4. vue 裁剪图片,插件Cropper的使用

    全局安装    npm install cropperjs 如果想本项目安装,方便移植:   import Cropper from 'cropperjs'   --save    这样的话,本地 p ...

  5. 51nod1331 狭窄的通道

    题目传送门 这道题 51nod只Ac了十二个人 没有题解可以研究 所以就自己YY了半天 在这里先感谢一波岚清大爷 orz 然后这道题我分了两种情况 一种是左边的往左跑右边的往右跑 中间有一部分直接走不 ...

  6. 20155330 2016-2017-2 《Java程序设计》第六周学习总结

    20155330 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 学习目标 理解流与IO 理解InputStream/OutPutStream的继承架构 理解 ...

  7. EF crud操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. dubbo集群服务下一台服务挂了对服务调用的影响

    一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...

  9. HashMap原理分析(JDK1.7.x之前)

    HashMap 实现Map.Cloneable.Serializable接口,继承AbstractMap基类. HashMap map = new HashMap<String,String&g ...

  10. configure编译选项

    1.rpath与rpath-link的区别 参考链接:http://blog.csdn.net/xph23/article/details/38157491 rpath 是 运行时候链接的库, rpa ...