代码之家  ›  专栏  ›  技术社区  ›  xain

Grails和多线程进程

  •  3
  • xain  · 技术社区  · 15 年前

    I have a grails app and I need to trigger an cpu-intensive process. I'm thinking in creating a service that spawns multiple threads that do the same calculations but with random data to compare the results later on. Is it safe to do it this way ? Any recommendations / experiences ?

    谢谢。

    2 回复  |  直到 14 年前
        1
  •  4
  •   Ted Naleid    14 年前

    The biggest problem you'll likely have is that any threads you create won't automatically have a hibernate session attached to them. So if you need to do anything with your domain, you'll need to wire up stuff manually. I looked into it a while ago and it was doable, but I ended up going another way so don't have a finished example to talk about.

    我想也有一些插件,比如 Background Thread plugin that will spawn a thread for you and attach the appropriate hibernate stuff. I'm not sure that the plugin is still maintained though so it's possible it won't work on 1.3 or later.

    您还可以使用withTransaction在线程中实例化有效的事务。 link text :

    MyDomain.withTransaction { status ->
        // GORM stuff that needs a valid transaction
    }
    

    Note that each thread would then have it's own transaction and that it wouldn't participate in the same transaction as the other threads (so you wouldn't be able to roll everything back if one thread fails).

        2
  •  1
  •   Jon Skeet    15 年前

    <plug>

    碰巧,我正在复习 second edition of Groovy in Action right now (as in I have the Word document open, and I'm editing it whenever I'm not posting here). That chapter covers concurrency in Groovy in general - it doesn't explicitly mention Grails, but I doubt that that has 许多的 impact on what you might want to use. Chapter 17 is already available in the early access "MEAP" edition...

    </plug>

    Anyway, I don't have any direct experience of concurrency in Groovy myself, but it sounds like you should be looking at GPars .