protected override void OnStart(String[] args)
{
ResultManager.PrepareCache();
ThreadPool.QueueUserWorkItem(ResultQueue.Process);
ThreadPool.QueueUserWorkItem(StatusUpdater.UpdateStatus);
ThreadPool.QueueUserWorkItem(GeneralQueue.RestartHungTests);
ThreadPool.QueueUserWorkItem(ResultManager.SyncroniseResultsTable);
ThreadPool.QueueUserWorkItem(GeneralQueue.RecoverLostResults);
ThreadPool.QueueUserWorkItem(BrowserTestStartInfo.FillQueues);
ThreadPool.QueueUserWorkItem(MailAppAccount.FillQueues);
}
Run Code Online (Sandbox Code Playgroud)
这些任务中的每一个都在Windows服务的生命周期内运行.我一直坚持使用ThreadPool来做这种事情,我应该只是开始正常的线程吗?我可以确定ThreadPool有足够的线程来运行每个任务吗?如果我将SetMaxThreads设置为7,我会在以后遇到问题,因为这些线程永远不会中止吗?将它设置为更高的东西是否安全?
我总是希望所有7个线程同时运行,它们永远不会中止 - 我是否应该使用线程?还有其他更适合这种永久性运行的任务吗?
每个任务每x分钟运行一个特定的方法.
我创建了一个Windows服务,目前有三个计时器.第一个计时器每15秒唤醒一次,第二个计时器每分钟唤醒一次.第三个计时器每天都在醒来.
问题是这些是每次都会产生新的线程,而且一次线程池完全用完了.这就是产生3个线程并且不会产生更多的新线程.
我的代码看起来像这样:
protected Onstart()
{
var timer1 = new TImer();
timer.Elapsed += Event1;
timer1.interval = 60000;
timer1.start();
var timer2 = new TImer();
timer2.Elapsed += Event2;
timer2.interval = 60000;
timer2.start();
}
private Event1(object,elapsedeventargs)
{
var workerthread1 = **new thread**(workerthreadfunc1)
workerthread1.start();
}
private Event2(object,elapsedeventargs)
{
var workerthread2 = **new thread**(workerthreadfunc2)
workerthread2.start();
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以看到它正在创建新线程,它将在某个时刻耗尽线程池中的所有线程并突然停止Windows服务.目前它正在停止并淹没活动ID为5000的evet日志.
示例线程池:
public class Example {
public static void Main() {
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 1
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 2
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 3
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
static void ThreadProc(Object stateInfo) {
Console.WriteLine("Hello from the thread pool.");
}
}
Run Code Online (Sandbox Code Playgroud)
示例信号量:
public class Example
{
private static Semaphore _pool;
private static int _padding;
public static void Main()
{
_pool = new Semaphore(0, 3);
// Create and start five numbered threads.
//
for(int i = …Run Code Online (Sandbox Code Playgroud) 是否有任何模拟Delphi指定某些线程是后台线程?至于.NET我可以说SomeThread.IsBackground = true;,这个线程将成为后台.提前致谢!
我必须以最快的方式复制文件的thousends.使用它是一个好主意ThreadPool吗?
我可以列入这么多物品吗?
更好的解决方案?
我想复制到不同的网络位置(随机).
我有一个TThreadList指向我的服务器创建的对象的所有指针.服务器有一个线程池,用于将数据发送到该线程列表中的所有客户端.它会锁定列表,然后在用户断开连接后释放列表,删除对象和引用指针.
问题是:使用不知道套接字何时断开连接和删除的线程池,因此当池工作时,它将从线程列表中的选定指针AV上以某种方式已被删除.
例如,池的Execute方法:
try
with userlist.LockList do
begin
for i := Count - 1 downto 0 do
begin
if i >= 0 then
begin
p := TClient(items[i]);
//problem here p gets deleted so it raises items[i]
//have an invalid pointer to memory
if p <> nil then
if p.sockethex <> nil then
begin
sendtexthexpool(p.sockethex, msgprimit+ '|'); //here AV cause P was deleted
end;
end;
end;
end;
finally
userlist.UnlockList;
end;
end;
Run Code Online (Sandbox Code Playgroud) 所以我试图在这里链接任务,但是在使用Linux下的GCC编译时,我收到警告:从不兼容的指针类型[默认启用]进行分配.即使我只是使用相同类型的指针.
typedef struct
{
void (*routine)(void*);
void* data;
struct p_task* next;
struct p_task* prev;
int deadline;
int timeWaiting;
}p_task;
typedef struct
{
pthread_t mainThread;
pthread_t* threadArray;
int threadCount;
p_task* firstInLine;
p_task* lastInLine;
}p_pool;
void pool_add_task(p_pool* pool, void* routine, void* data)
{
// create new task
p_task* task = malloc(sizeof(p_task));
task->routine = routine;
task->data = data;
task->deadline = 5;
task->timeWaiting = 0;
// when no tasks are chained yet
if (pool->firstInLine == NULL)
{
pool->firstInLine = task;
pool->lastInLine = task;
} …Run Code Online (Sandbox Code Playgroud) 我正在研究一个MultiThreading c#教程,我还没有完全理解有关进程和线程的整个图片.我已经明白线程在这个过程中,但不清楚什么是一个过程?是类的实例,特定方法和整个程序集文件,它是什么?当我看到Lock和Mutex之间的区别时,这个疑问就出现了.Mutex类的定义是......"一个同步原语,也可以用于进程间同步......"以及后来...... Mutex是一个同步原语,它只允许对一个线程的共享资源进行独占访问,这让我有点困惑?我说错了是因为Mutex会同步来自不同进程的线程吗?
关于ThreadPool,定义是......"线程池是一组线程,可用于在后台执行多个任务...."这个线程集合来自不同进程的相同进程或线程?
如何等待2分钟完成方法,但如果没有完成则退出并继续?
我想等待2分钟才能完成方法,但是如果它在2分钟内没有完成,那么退出执行并继续前进.
我在MacOS上运行dotnet核心版本2.1.3,并且尝试使用默认线程池进行操作没有成功。我试图以几种方式修改线程数(其中大多数是我用谷歌搜索的):
Program.cs
public static void Main(string[] args)
{
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(1, 1);
DoSomethingAsync();
}
Run Code Online (Sandbox Code Playgroud)
.csproj
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<RetainVMGarbageCollection>true</RetainVMGarbageCollection>
<ThreadPoolMinThreads>1</ThreadPoolMinThreads>
<ThreadPoolMaxThreads>1</ThreadPoolMaxThreads>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
环境变量
$ ComPlus_ThreadPool_ForceMinWorkerThreads=1 ComPlus_ThreadPool_ForceMaxWorkerThreads=1 dotnet run
Run Code Online (Sandbox Code Playgroud)
这些似乎都不起作用:
$ ps -M 97046
USER PID TT %CPU STAT PRI STIME UTIME COMMAND
XXX 97046 s002 0.0 S 31T 0:00.02 0:00.07 dotnet exec /XXX
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S …Run Code Online (Sandbox Code Playgroud)