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. 网络编程基础【day09】:实现简单地ssh(四)

    本节内容 概述 简单ssh socket接收大数据的困惑 一.概述 我们用过linux的就知道什么是ssh,它是一种客户端和服务端交互返回的一个解决,输入一个命令,给我返回什么,接下来我们说一说,如何 ...

  2. Spring+quartz 实现定时任务job集群配置【原】

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  3. Spring面向切面编程AOP(around)实战

    spring aop的环绕通知around功能强大,我们这里就不细说,直接上代码,看着注释就能明白 需要的可以点击下载源码 1.如果使用注解的方式则需要先创建个注解类 package com.mb.a ...

  4. go通过swig封装、调用c++共享库的技术总结

    go通过swig封装.调用c++共享库的技术总结 @(知识记录) 1 简介 最近在研究golang,希望能对目前既有的python服务做一些优化,这些服务目前已经占用了6-7台机器.选择golang的 ...

  5. DPM 目标检测1

    1. Origin 原始目标检测: HOG梯度模型+目标匹配 为了提过对目标形变的鲁棒性(多视角->多组件): 目标形态多样性—>多个模型 目标的动态变化多视角—> 子模型 目标形变 ...

  6. 矩阵NumPy

    常量: np.pi π 创建矩阵数组 import numpy as np # array=np.array([[1,2,3],[5,6,7]]) #定义一个2行3列的矩阵数组.2行=2维 # pri ...

  7. UDP网络程序,客户端和服务端交互原理

    创建一个udp客户端程序的流程是简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实 ...

  8. android 使用SQLite存储数据

    创建一个类继承SQLiteOpenHelper,重写他的构造方法.onCreate().onUpgrade() 构建出SQLiteOpenHelper实例后,再调用他的getReadableDatab ...

  9. 2017/05/03 java 基础 随笔

    1.硬盘500G 厂商是按照1000计算的 500g=500*1000*1000/1024/1024=465g 2.jdk1.7可以表示二进制了 0b001(b大小写无所谓) 3.进制转换 4.原码, ...

  10. Docker镜像命令

    ①docker images [Options] 用途:列出本地主机上的镜像 Options说明: -a:列出本地所有的镜像(含中间映像层) -q:只显示镜像ID --digests:显示镜像的摘要信 ...