using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ThreadStart thr_start_func = new ThreadStart(First_Thread);
Console.WriteLine("Creating the first thread ");
Thread fThread = new Thread(thr_start_func);
fThread.Name = "first_thread";
fThread.Start(); //starting the thread
Thread sThread = new Thread(new ThreadStart(Second_Thread));
sThread.Name = "second_thread";
sThread.Start();
} public static void First_Thread()
{
Console.WriteLine("First thread created");
Thread current_thread = Thread.CurrentThread;
string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
Console.WriteLine("The details of the thread are :" + thread_details);
Console.WriteLine("first thread terminated");
} public static void Second_Thread()
{
Console.WriteLine("Second thread created");
Thread current_thread = Thread.CurrentThread;
string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
Console.WriteLine("The details of the thread are :" + thread_details);
Console.WriteLine("second thread terminated");
}
}
}
//如果不是,谁能提供一个简单的、多线明确的源代码示例?
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ThreadStart thr_start_func = new ThreadStart(First_Thread);
Console.WriteLine("Creating the first thread ");
Thread fThread = new Thread(thr_start_func);
fThread.Name = "first_thread";
fThread.Start(); //starting the thread
Thread sThread = new Thread(new ThreadStart(Second_Thread));
sThread.Name = "second_thread";
sThread.Start();
} public static void First_Thread()
{
Console.WriteLine("First thread created");
Thread current_thread = Thread.CurrentThread;
string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
Console.WriteLine("The details of the thread are :" + thread_details);
Console.WriteLine("first thread terminated");
} public static void Second_Thread()
{
Console.WriteLine("Second thread created");
Thread current_thread = Thread.CurrentThread;
string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
Console.WriteLine("The details of the thread are :" + thread_details);
Console.WriteLine("second thread terminated");
}
}
}
//如果不是,谁能提供一个简单的、多线明确的源代码示例?
解决方案 »
- 显示程序的系统托盘图标
- ditagrid绑定list的问题
- 关于datepick控件的一个问题 急求。。。。。。。。。。在线等待
- 客户端运行后会自动生成一个文件夹(没有子文件夹),里面全部是txt文档,我希望在客户端关闭前将这个文件夹传送到服务器端,用web service做
- 求助:ASP.NET中如何正确读取由C++中定义的结构写成的二进制文件
- 有一控件我使用了其MouseDown事件后我又想使用其DoubleClick事件该怎么做?
- 在visual C# .net的水晶报表怎样导出到Excel,同时可以把线条也导出
- 三层开发中的WEB引用问题
- 多任务并行处理的管理类设计
- 急,为什么会出现这样的资源异常?50分相送!
- 汉字乱码问题!!!
- 使用SMO运行insert脚本速度很慢
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace WZDM.Test
{
/// <summary>
/// 限制并发线程数例程
/// </summary>
public class TestThread
{
int _currentThreads = 0;
int _maxThreads = 10; public void Test()
{
while (true)
{
//_maxThreads = new Random().Next(1, 10);
for (int i=0; i<100; i++)
{
// 在此进行数量限制
if (_currentThreads >= _maxThreads)
break; // 开启线程
lock (typeof(TestThread))
{
_currentThreads++;
if (_currentThreads > _maxThreads)
_currentThreads = _maxThreads; string currentInfo = string.Format("thread {0}/{1}", _currentThreads, _maxThreads);
Console.WriteLine(currentInfo + " start");
Thread thread = new Thread(new ParameterizedThreadStart(Run));
thread.Start(currentInfo);
}
} System.Threading.Thread.Sleep(2000);
}
} // 线程任务
void Run(object obj)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("{0}:{1}", obj, i);
Thread.Sleep(100);
}
Console.WriteLine("{0} finished", obj);
lock (typeof(TestThread))
{
_currentThreads--;
if (_currentThreads < 0)
_currentThreads = 0;
}
}
}
}
分3个线程你的在主线程执行 这个现成会关心另外2个执行 First_Thread,Second_Thread() 执行的怎么样了,主线程序会继续执行后面的. Second_Thread是不关心 First_Thread是否执行完就会开始执行
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace thread
{
class Program
{
static void Main(string[] args)
{
Thread T1 = new Thread(new ThreadStart(proces.pro1));
Thread T2 = new Thread(new ThreadStart(proces.pro2));
T1.Start();
T2.Start();
T1.Join();
T2.Join();
Console.WriteLine("主程序运行完毕");
}
} public static class proces
{
public static void pro1()
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(1);
Console.WriteLine("线程1运行中...");
}
Console.WriteLine("线程1运行完毕...");
} public static void pro2()
{
for (int i = 0; i < 20; i++)
{
Thread.Sleep(1);
Console.WriteLine("线程2运行中...");
}
Console.WriteLine("线程2运行完毕...");
}
}
}
两个任务同时进行,主线程等两个子任务完成再结束整个程序。