线程的两种启动方式
2009-12-07 21:55
330 查看
1、没有参数的启动方式:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace MoreThead
{
class TheadProgram
{
Thread th;
static void Main(string[] args)
{
TheadProgram tp = new TheadProgram();
tp.RunThread();
}
private void RunThread()
{
//启动一个不带参数的线程
th = new Thread(new ThreadStart(Sum));
th.Start();
}
private void Sum()
{
//一个简单的加一操作
int iBegin = 0;
iBegin++;
Console.Write("{0}", iBegin);
}
}
}
2、含参数的启动方式:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace MoreThead
{
class TheadProgram
{
Thread th;
static void Main(string[] args)
{
TheadProgram tp = new TheadProgram();
tp.RunThread();
}
private void RunThread()
{
//启动一个带参数的线程
th = new Thread(new ParameterizedThreadStart(Sum));
th.Start(5);
}
private void Sum(object obj) //注意这里的参数类型要为 object;可以在方法体内把参数转化成实际的类型
{
//一个简单的加一操作
int iBegin = int.Parse(obj.ToString());
iBegin++;
Console.Write("{0}", iBegin);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace MoreThead
{
class TheadProgram
{
Thread th;
static void Main(string[] args)
{
TheadProgram tp = new TheadProgram();
tp.RunThread();
}
private void RunThread()
{
//启动一个不带参数的线程
th = new Thread(new ThreadStart(Sum));
th.Start();
}
private void Sum()
{
//一个简单的加一操作
int iBegin = 0;
iBegin++;
Console.Write("{0}", iBegin);
}
}
}
2、含参数的启动方式:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace MoreThead
{
class TheadProgram
{
Thread th;
static void Main(string[] args)
{
TheadProgram tp = new TheadProgram();
tp.RunThread();
}
private void RunThread()
{
//启动一个带参数的线程
th = new Thread(new ParameterizedThreadStart(Sum));
th.Start(5);
}
private void Sum(object obj) //注意这里的参数类型要为 object;可以在方法体内把参数转化成实际的类型
{
//一个简单的加一操作
int iBegin = int.Parse(obj.ToString());
iBegin++;
Console.Write("{0}", iBegin);
}
}
}
相关文章推荐
- 线程 创建和启动线程的两种方式 实现Runnable接口 继承Thread类 重写唯一方法run()
- java中启动线程的两种方式
- 启动线程的两种方式
- java 启动线程的两种方式
- 创建和启动线程的两种方式
- Java多线程之两种启动线程方式
- java实现线程的两种方式
- Android Service的两种启动方式
- java线程实现的两种方式实例
- java 启动线程三种方式
- 线程的两种实现方式
- 【多线程】-线程范围内共享数据的两种方式
- windows phone 8 新增功能:从一个应用程序启动另一个程序(file association 和 Protocol association两种方式)
- Service 两种启动方式
- java多线程总结一:线程的两种创建方式及优劣比较
- Android Service两种启动方式
- 线程间协作的两种方式
- 开辟线程的两种方式
- Eclipse Mars.2 Release (4.5.2) 启动web项目的两种方式
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition