Why does the JVM not have a GIL, while the Ruby and Python interpreters do?
-
I understand that Ruby and Python interpreters need GIL for thread safety. Removing it would mean that all their extensions need to be thread safe. How is it that the JVM never had this problem?
-
Answer:
It's a question of whether you want to use one big, fat lock (aka GIL) or use lots of fine-grained locks. The former is easier to implement compared to the latter. If you want to know more about CPython's GIL, you should check out David Beazley's GIL talks ([1] & [2]). I think video recordings of his talks are available on Youtube as well. Btw in CPython extensions, you're allowed to release GIL, do a bunch of computation and let other Python threads do their job. When you're done with the computation, you can reacquire the GIL and proceed as normal. This is what CPython does whenever you do blocking syscalls. Lots of extensions like NumPy release the GIL, do a bunch of computation with multiple threads. But GIL becomes a huge issue if you're trying to do lot of CPU-intensive work in pure Python code. [1] - http://www.dabeaz.com/python/UnderstandingGIL.pdf [2] - http://www.dabeaz.com/python/GIL.pdf
Harish Mallipeddi at Quora Visit the source
Other answers
Python and Java had different design goals. The GIL didn't violate the set of highest-priority Python design goals, while it would have devastated the Java design goals. That doesn't make one better than the other; they are both beautiful languages in their own ways. Had many-core and many-thread processing been a given when Python was designed (and it is older than Java!), there would be a different set of engineering trade-offs, and likely no GIL.
Cameron Purdy
It's a design decision. Either give the global lock to the running thread, or lock a resource that a thread is using. The jvm uses the latter approach. The JVM implementation of python (jython) does not introduce a GIL. I'm not sure if the same is true for JRuby. It's a very tough decision to make. Since threads are handled differently per OS, the implementors have to prioritize between the two: a cross-platform language or a cross-platform execution environment. It's already a tremendous effort to implement a language, much more for an execution environment.
Evan Dale Aromin
Java was designed from the beginning (more or less) to be multithreaded. A global lock makes multithreading impossible; it means that some thread can hog the system and cause resource starvation. Even system threads could potentially be starved. GILs are simple to implement, and effective, but limited. The Java designers had much higher hopes for their threading model.
Joshua Engel
Related Q & A:
- Does anyone know why sometimes I pig out for a few days, then other days i can't eat anything at all?Best solution by Yahoo! Answers
- Why is the sky green during a tornado?Best solution by Yahoo! Answers
- Why is it that all of a sudden I cannot insert pictures into my email?Best solution by Yahoo! Answers
- Why is it important to have a Public Relations campaign?Best solution by wemagazineforwomen.com
- Why do I get the error "A network error occured while connecting to the server?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.