线程化,如何正确执行并行任务 [英] Threading, how to do it right for parallel tasks

查看:62
本文介绍了线程化,如何正确执行并行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨同胞CP'ians,



我在考虑多用户软件时遇到了一个新问题。 Sohort背景故事......



我记得,升级的多用户系统应该并且将与观察者一起运行,以便为软件提供按摩和更新。但是目前我没有得到时间段来实现它,这就是为什么我继续使用锁(存储到数据库中)的另一种方法。



Soooo,现在我正在考虑不同的场景,比如项目经理将项目设置为编辑模式(为项目中的每个对象设置锁定),并且有人已在处理对象。在这种情况下,我不能为PM锁定它并且必须通知他(将使用一个小图标)但我必须重新检查,直到另一个家伙关闭他的编辑并且PM最终可以锁定它。



为此,我想使用一个单独的线程来定期查询数据库,直到可以锁定对象。



现在我的问题:



因为我用线程工作了一点,我意识到它赢了'如果你留下一个创建线程的方法,那么工作顺利。所以只为我输入。我的感觉是对的吗?



我会尝试设置这样的



Hi fellow CP'ians,

i encountered a new problem while thinking about multi user software. Sohort background story...

I have in mind that the upcomming multiuser system should and will run with an observer to dedicate massages and updates to the software. But currently i don't get the timeslot to implement that, that's why i went on another approach using locks (that get stored into the DB).

Soooo, now im thinking about different scenarios like a project manager sets the project into edit mode (locks get set for every object in the project) and there is someone already working on an object. In this case i can't lock it for the PM and have to notify him (will use a small icon) but i have to recheck till the other dude closed his edit and the PM can finally lock it.

For this i wanted to use a seperate thread that querys the DB regularly till the object can be locked.

Now to my Question:

Since i worked a bit with threads i realised that it won't work that smooth if you leave a method in which you created a thread. So just for me as input. Is my feeling right?

I would try to set up like this

public bool LockProject(GUID InProjectID)
{
    bool _result = false;

    //Lock the Project
    /*Do some code*/

    //Check if locks can't be set and remove the double locks
    /* Do some code*/

    //Start the Thread to continuously ask if a lock still persists
    /*Initialize Thread and let it run the check method*/

    return _result;
}





那么这会杀死我的线程吗?我希望它能返回true,因为我们可以为其他人锁定项目但仍需要检查。所以,如果我离开这个方法,那么Thread会被垃圾收集器抛弃还是会有效?



否则,我必须把线程放在哪里?这可以通过这个任务来实现我阅读这里?



我不需要代码,只需向正确的方向推送,或者我的想法是正确的确认。



在此先感谢:)



我尝试过:



As如上所述,关于如何处理这个问题的想法。



So would this "kill" my thread? I expect it to return true, since we could lock the project for others but still need to check. So if i leave the method, will the Thread get dumped by the garbage collection or would this work?

Otherwise, where would i have to put the Thread? Is this realisable with this Task thing i read here?

I don't need code, just a push in the right direction or if my thoughts are correct a confirmation.

Thanks in advance :)

What I have tried:

As written above, the thoughts about how to handle this issue.

推荐答案

我同意约翰尼斯的观点,即乐观的持久性是一个表现更好的模型,但如果你确信你想要实现锁定我会以稍微不同的方式做到这一点。



当你想要锁定实体时,你的锁应该获得锁定10秒钟5秒钟将重复此过程并续订收购。如果遇到任何未处理的异常或未能释放,该对象将在10秒后自动释放,无论如何。 (或者说有意义的时间段)。



当然如果你想跨会话锁定一个实体,这不适合你的方法,但值得考虑一下分布式锁定,故障恢复的工作原理。
I agree with johannes that an optimistic persistance is a better performing model however, if you are convinced that you want to implement locking I would do it in a slightly different way.

When you want to lock the entity, your lock should acquire the lock for 10 seconds, and every 5 seconds would repeat the process and renew the acquisition. If you encounter any unhandled exception, or failure to release, the object will be automatically released after 10 seconds regardless. (or a time period that makes sense).

Of course if you want to lock an entity across sessions, this is not the approach for you, but worth considering with distributed locking, how failure recovery works.


这篇关于线程化,如何正确执行并行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆