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. Scala进阶之路-正则表达式案例

    Scala进阶之路-正则表达式案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 废话不多说,正则大家都很清楚,那在Scala如何使用正则了?我们直接上个案例,如下: /* @au ...

  2. SVN的Windows和Linux客户端操作详解

    SVN的Windows和Linux客户端操作详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Windows客户端操作 1.安装SVN客户端 a>.去官网下载svn软件 ...

  3. JavaSE学习总结(十)—— JDBC与面向对象测试

    一.题目 请使用MySQL+JDBC+JAVASE控制台完成一个图书管理系统(Libs),实现添加图书,查询图书,根据图书编号查询图书三个功能. 二.要求 1.必须有菜单 2.至少3个以上的字段(编号 ...

  4. nginx的负载均衡配置,常用策略

    场景:nginx是一款非常优秀的负载均衡服务器,小巧而且性能强悍,中小型企业的首选. 下面介绍nginx的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...

  5. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  6. C# 网络常用操作类NetHelper.cs

    一个非常完整的网络操作帮助类,包含20多个常用方法,例如: IP地址的验证以及截取. 端口的验证. 电子邮件的发送. 获取计算机名. IP地址的获取以及TCP. UDP连接的创建和数据发送等. usi ...

  7. WorkerMan 入门学习之(三)基础教程-Timer类的使用

    1.ServerTimer.php 代码: <?php /** * 定时器学习 */ require_once __DIR__ . '/Workerman/Autoloader.php'; us ...

  8. Gitlab配置阿里邮件通知

    1. 在/etc/gitlab/gitlab.rb 中添加如下内容 $ vi /etc/gitlab/gitlab.rb gitlab_rails['smtp_enable'] = true  git ...

  9. 通用Excel文件导出工具类

    1:Excel格式 2:ExcelUtil.java import java.io.ByteArrayOutputStream; import java.io.IOException; import ...

  10. vue.js初始学习笔记&vue-cli

    笔记一下: vue.js 安装,参考: http://www.cnblogs.com/wisewrong/p/6255817.html (vue-cli) http://www.cnblogs.com ...