• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads: Make a framework or build it in? Why diff in books/production?

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I EVERY single book I have read on Multithreading in Java, the authors have created (and strongly suggested using) a library/framework for use with threaded applications. They all recommend that you handle locking/unlocking of mutexes and wait/notify in a library that can be reused with all applications needing such functionality.
HOWEVER, in almost every single piece of production code (including sun's JRE implementation), this is NEVER the case. Instead, all code dealing with synchronization, locking, wait/notify is all handled directly in whatever code needs such functionality. Why is this? Which is the "better" route and why?
I know there is always difference between books and reality, but I cannot think of any other area in Java development with more disconnect between the academic (i.e. book) world and the "real" production world. Why? What happened?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know why one would create a redundant framework for threads. In fact it is quite impossible because you can not create any thread safe behaviors without the usage of the existing JVM thread releated commands.
use of a library has nothing to do with wether or not your program is threaded. Perhaps you have misinterpreted?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Framework is a loaded term, one that can start serious battles over the definition, so I'll avoid it. You can certainly make a handy set of utilities for dealing with threads like lockers, blockers, pools, and more. JDK 1.5 (in alpha?) will have a bunch of new thread goodies. I made an elaborate scheduler and then found that Timer does roughly the same thing right out of the JDK. Sigh.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question, and I don't have a good answer, overall. However I do think that in the last couple years, a significant factor may be that many of the people who might have developed such libraries were also aware of Doug Lea's efforts and the fact that they were being incorporated (eventually) in the JDK. While of course it did take a while for this to arrive (to the extent that the JDK 1.5.0 beta release is now "arrived") I think that even before Tiger was available, the knowledge that a more official library was coming down the line may have diminished the incentive for other people to provide a good alternate library. After all, Lea's util.concurrent has been available for a long time as a separate library, and it's long been known that there were plans to put something like util.concurrent into the core Java library.
Another factor is the fact that the difficulties of thread-unsafe programs are not readily apparent to people. A thread-unsafe program may well perform correctly 99.9% of the time - and when it fails, it's often in a manner that the programmer can't easily debug. So many programmers simply have no idea that their programs are unsafe - if if they have an idea, they don't realize that threading issues are the problem. There are many people out there who seem to think that using the word "synchronized" somewhere in the program makes it magically thread-safe, without understanding the details. In other cases, programmers are making a conscious decision to tolerate occasional errors in return for greater performance, by omitting "synchronized" altogether (or omitting it in certain circumstances). Either way, most programmers are not conscious of any real problems with the standard Java threading pardigm. And those who are conscious of the potential problems are either good enough to avoid the problems, or overconfident enought to think they can avoid them.
Well, those are my own guesses anyway. Hope that helps...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!