C# C语言中的线程概念#
我是C#新手,仍在学习线程概念。我写了一个程序如下C# C语言中的线程概念#,c#,multithreading,C#,Multithreading,我是C#新手,仍在学习线程概念。我写了一个程序如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApplication17 { class Program { static void Main(string[]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
System.Threading.ThreadStart th1 = new System.Threading.ThreadStart(prnt);
Thread th = new Thread(th1);
th.Start();
bool t = th.IsAlive;
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i + "A");
}
}
private static void prnt()
{
for (int i = 0; i < 10; i ++)
{
Console.WriteLine(i + "B");
}
}
}
}
因为我认为主线程和新创建的线程应该同时执行
但是输出是有序的,首先调用
prnt
函数,然后执行循环的Main
方法。可能10的圆圈不足以看到两个线程同时运行。
使用更长的循环或在循环迭代中放置一个Thread.Sleep(100)。
开始一个线程是相当昂贵的。可能发生的情况是,由于线程启动所需的时间,主循环在线程循环启动之前执行我同意前面的答案,您需要添加thread.Sleep(几秒*1000) 我建议您阅读这篇关于线程的文章
static void Main(字符串[]args)
{
螺纹th=新螺纹(prnt);
th.Start();
对于(int i=0;i<10;i++)
{
//睡一会儿
睡眠(1000);
控制台写入线(i+“A”);
}
//连接基本线程和“th”线程
th.Join();
}
私有静态无效prnt()
{
对于(int i=0;i<10;i++)
{
//睡一会儿
睡眠(1000);
控制台写入线(i+“B”);
}
}
已更新:进程用于隔离在该进程上下文中运行的应用程序和线程
这样,操作系统就更容易管理不同的应用程序,管理崩溃和上下文切换(CPU为每个应用程序提供的时间段)。背后的想法就是在预定义的时间段内运行线程,当应用程序允许运行的时间结束时,CPU切换到其他线程执行
对于应用程序中的小型异步任务,请使用Threading类
请参见下面的示例中如何使用System.Threading命名空间创建新线程。请注意如何调用t.Join()以等待新线程完成
请注意,Spleep(1)将强制更改上下文,这就是您的示例的不同之处。尝试将其更改回零,然后查看结果
using System;
using System.Threading;
namespace Chapter1
{
public static class Threads1
{
public static void ThreadMethod()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(1);
}
}
public static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start();
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(1);
}
t.Join();
Console.Read();
}
}
}
// OUTPUT:
//Main thread: Do some work.
//ThreadProc: 0
//ThreadProc: 1
//Main thread: Do some work.
//ThreadProc: 2
//Main thread: Do some work.
//ThreadProc: 3
//Main thread: Do some work.
//ThreadProc: 4
//ThreadProc: 5
//ThreadProc: 6
//ThreadProc: 7
//ThreadProc: 8
//ThreadProc: 9
使用系统;
使用系统线程;
名称空间第1章
{
公共静态类Threads1
{
公共静态void ThreadMethod()
{
对于(int i=0;i<10;i++)
{
WriteLine(“ThreadProc:{0}”,i);
睡眠(1);
}
}
公共静态void Main(字符串[]args)
{
螺纹t=新螺纹(新螺纹起点(螺纹方法));
t、 Start();
对于(int i=0;i<4;i++)
{
WriteLine(“主线程:做一些工作”);
睡眠(1);
}
t、 Join();
Console.Read();
}
}
}
//输出:
//主线:做一些工作。
//ThreadProc:0
//ThreadProc:1
//主线:做一些工作。
//ThreadProc:2
//主线:做一些工作。
//ThreadProc:3
//主线:做一些工作。
//ThreadProc:4
//ThreadProc:5
//ThreadProc:6
//ThreadProc:7
//ThreadProc:8
//ThreadProc:9
您的代码看起来不错;大概线程启动所需的时间比主线程完成所需的时间更长。添加线程睡眠(500)
到每个用于循环;线程调度是不可预测的,许多程序都会失败(竞态条件),因为程序员假设了一些不存在的关于线程的理想行为,例如假设线程总是以并行和完全同步的方式运行。他们没有。考虑你的程序是如何运行在一个带有大(100MS+)时间切片的OS的单线程处理器上的。一个“线程”用来隔离应用程序并让它们在自己的进程中运行。不,线程不是进程,它不会隔离“应用程序”。线程是一个执行单元,允许并行完成多个工作。线程仍在进程的逻辑上下文中。更新@YuvalItzchakov
static void Main(string[] args)
{
Thread th = new Thread(prnt);
th.Start();
for (int i = 0; i < 10; i++)
{
//sleep one second
Thread.Sleep(1000);
Console.WriteLine(i + "A");
}
//join the basic thread and 'th' thread
th.Join();
}
private static void prnt()
{
for (int i = 0; i < 10; i++)
{
//sleep one second
Thread.Sleep(1000);
Console.WriteLine(i + "B");
}
}
using System;
using System.Threading;
namespace Chapter1
{
public static class Threads1
{
public static void ThreadMethod()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
Thread.Sleep(1);
}
}
public static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start();
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(1);
}
t.Join();
Console.Read();
}
}
}
// OUTPUT:
//Main thread: Do some work.
//ThreadProc: 0
//ThreadProc: 1
//Main thread: Do some work.
//ThreadProc: 2
//Main thread: Do some work.
//ThreadProc: 3
//Main thread: Do some work.
//ThreadProc: 4
//ThreadProc: 5
//ThreadProc: 6
//ThreadProc: 7
//ThreadProc: 8
//ThreadProc: 9