对线程的一点点新理解
来源:互联网 发布:忘仙商人软件 编辑:程序博客网 时间:2024/05/19 17:47
最近在做一个提示框,要用到线程,遇到的问题是不知道怎么在一个类中用同一个线程来调用所有方法。
后来想,是不是只要在线程中创建了这个类,那这个类里的所有原生的东西都属于这个线程。
于是写了个简单的程序试了下:
public class Class1 { public void Method1() { Console.WriteLine("{0} - {1} - {2}", "Class1", "Method1", Thread.CurrentThread.ManagedThreadId); } public void Method2() { Console.WriteLine("{0} - {1} - {2}", "Class1", "Method2", Thread.CurrentThread.ManagedThreadId); } public void Method3() { Console.WriteLine("{0} - {1} - {2}", "Class1", "Method3", Thread.CurrentThread.ManagedThreadId); } }
public class Class1Caller { public void CallClass1() { Task.Factory.StartNew( delegate { Console.WriteLine("{0} - {1} - {2}", "Class1Caller", "CallClass1", Thread.CurrentThread.ManagedThreadId); Class1 c1 = new Class1(); c1.Method1(); c1.Method2(); c1.Method3(); }); } }
public class Class2 { public void CallClass2() { this.Method1(); this.Method2(); this.Method3(); } private void Method1() { Task.Factory.StartNew( delegate { Console.WriteLine("{0} - {1} - {2}", "Class2", "Method1", Thread.CurrentThread.ManagedThreadId); }); } private void Method2() { Task.Factory.StartNew( delegate { Console.WriteLine("{0} - {1} - {2}", "Class2", "Method2", Thread.CurrentThread.ManagedThreadId); }); } private void Method3() { Task.Factory.StartNew( delegate { Console.WriteLine("{0} - {1} - {2}", "Class2", "Method3", Thread.CurrentThread.ManagedThreadId); }); } }
class Program { static void Main(string[] args) { Class1Caller c1c = new Class1Caller(); Class2 c2 = new Class2(); c1c.CallClass1(); c2.CallClass2(); Console.ReadLine(); } }
测试结果:
证明我的设想是正确的。
0 0
- 对线程的一点点新理解
- 对Cocoa中新建线程的一点点理解
- 对jsp的一点点理解
- 对指针的一点点理解
- 对static的一点点理解
- 对Thrift的一点点理解
- 对Thrift的一点点理解
- 对Dijkstra的一点点理解。
- 对线程新的自我理解
- 对MFC原理的一点点理解
- 我对搜索算法的一点点理解
- 对局部性原理的一点点理解
- 对JS闭包的一点点理解
- 对线程的理解
- 对Oracle中分组函数的一点点理解
- 对指针运算符*文字说明的一点点理解
- 我对C++ Traits编程技法的一点点理解
- 对优先队列的一点点理解,文采学识有限,见谅!
- PHP发送POST请求的三种方式
- 数据的大端小端表示法
- extjs 表格单元格 垂直居中对齐
- 20160324servlet学习笔记HttpServletRequest对象中文乱码解决方法
- 3月24号
- 对线程的一点点新理解
- 什么是类型安全的
- hdu 4747 线段树
- HDOJ 2200 Eddy's AC难题(数学组合概率题)
- ural 1013. K-based Numbers. Version 3
- 细说dex2oat(1)
- C++类的特性相关题目
- ORACLE 交叉报表固定列实现方法
- hdu1015(dfs)