C#:线程和多线程的概念
我已经在编写一些代码一段时间了。我有一个问题: 在C#中,“线程”和“多线程”是什么意思?C#:线程和多线程的概念,c#,C#,我已经在编写一些代码一段时间了。我有一个问题: 在C#中,“线程”和“多线程”是什么意思? 线程的作用是什么?看看这些文章,或者线程是在进程上下文中执行的一系列指令。当一个程序使用多个执行线程,允许每个线程根据分配给这些线程的优先级并发共享CPU时,就可以实现多线程 您可以参考此代码来学习线程 using System; using System.Threading; public class Alpha { // This method that will be called whe
线程的作用是什么?看看这些文章,或者线程是在进程上下文中执行的一系列指令。当一个程序使用多个执行线程,允许每个线程根据分配给这些线程的优先级并发共享CPU时,就可以实现多线程 您可以参考此代码来学习线程
using System;
using System.Threading;
public class Alpha
{
// This method that will be called when the thread is started
public void Beta()
{
while (true)
{
Console.WriteLine("Alpha.Beta is running in its own thread.");
}
}
};
public class Simple
{
public static int Main()
{
Console.WriteLine("Thread Start/Stop/Join Sample");
Alpha oAlpha = new Alpha();
// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
// Start the thread
oThread.Start();
// Spin for a while waiting for the started thread to become
// alive:
while (!oThread.IsAlive);
// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);
// Request that oThread be stopped
oThread.Abort();
// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();
Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");
try
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException)
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
return 0;
}
}
这是一个非常普遍和基本的问题。在谷歌上搜索这个问题,我相信你会得到很多关于这个问题的信息。这个问题已经用很多很好的方法回答了很多次,请至少先用谷歌搜索一下。阅读所有相关内容: