Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Are threads evil?  RSS feed

 
Didier Varon
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this question has already posted, but when I was in college my OO design and programming professor always made reference that Java threads are evil.

I understand that one can not rely on threads behavior because it depends on the JVM implementation and OS scheduler algorithm, and that a programmer does not control which method is dispatch on the thread, but still I don't have a clear understanding why Java threads are evil?

Can someone explain that please?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Didier.
when I was in college my OO design and programming professor always made reference that Java threads are evil.


That's not the strangest thing I've ever heard a professor say, but I have a degree in Literature. Either your professor was making reference to some specific problem when he made this assertation (most likely), he did not quite grasp the concept of multithreading (less likely) or your professor should be restricted from using sharp instruments lest he harm himself (you answer that).
There is nothing inherently evil about Java threads. Period.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I think the best thing I can tell you is not to believe everything you hear. Not only is there nothing intrinsically evil about threads in general or Java Threads in particular, Java's built-in threading support is one of the great features of the language. As for the claim that "a programmer does not control which method is dispatch on the thread" -- that's just completely wrong. You have precise control over exactly what code runs in each thread.

Watch out for wooden nickels!
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also like to add that Threads are very important in a lot of types of applications that without Threads, would not be possible to create.

Like that great two player game that uses Bluetooth to enable two cell phones to play against each other. Without Threads, this game would not be possible to create.

There is a saying that the greatest tool can be mis-used by the naive bypasser and become evil. OK, there is not a saying like this, and I made that sentence up. But really, having a little knowledge of something is sometimes dangerous. You really do need to understand Threads to use them correctly, and using them incorrectly will cause evil to occur.

With that said, The current 1.4 and before method of Threads was very adequate and powerful. But even now, the Java 5.0 Concurrency API package, opens up the world of Threads to greater and more powerful posibilities.

Good Luck

Mark
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Didier VAron:
I don't know if this question has already posted, but when I was in college my OO design and programming professor always made reference that Java threads are evil.


Sounds similar to one of my professors who said pointers are a horror in C. I almost always disagreed with him.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Annie Smith:


Sounds similar to one of my professors who said pointers are a horror in C. I almost always disagreed with him.


I wouldn't equate those two examples. Perhaps his professor just has a fondness for windows 3.1.
 
Saurabh Dixit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Threads are one of the most interesting feature in Java. What really makes it interesting is the ease with which we can do multithreading in java as compared to any other language. You can achieve a lot of complicated operations using threads.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've worked with multithreaded code quite a bit, and I think I understand what the professor meant.

In general, Java is a very "safe" language: singly threaded code has few or no dependencies on the environment in which it is run. Indeed, much of the point of Java was to provide a language that was 'write once, run anywhere'. This greatly simplifies testing and debugging, since you don't have to worry about testing on every single possible platform, and you don't have to duplicate a user's exact setup to duplicate his bugs.

Unfortunately, Java threads do not have such guarantees. The behavior of threads in Java is highly platform dependent, and code that always works perfectly on one machine may fail in unexplained ways on a different machine or operating system. In particular, the memory model, which does not require cache coherency, allows almost anything to happen if you aren't extremely careful - and those problems are nearly impossible to test for, since many of the problems that could come up on some platforms, such as those without cache coherency, cannot come up on many development platforms.

I think the simile to C pointers is an apt one. To the extent that the primary benefit of Java is to remove "evil" undefined behavior such as that associated with wild pointers in C, Java threads are "evil". Sometimes they are a necessary evil, though.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Dew:
I've worked with multithreaded code quite a bit, and I think I understand what the professor meant.

In general, Java is a very "safe" language: singly threaded code has few or no dependencies on the environment in which it is run. Indeed, much of the point of Java was to provide a language that was 'write once, run anywhere'. This greatly simplifies testing and debugging, since you don't have to worry about testing on every single possible platform, and you don't have to duplicate a user's exact setup to duplicate his bugs.

Unfortunately, Java threads do not have such guarantees. The behavior of threads in Java is highly platform dependent, and code that always works perfectly on one machine may fail in unexplained ways on a different machine or operating system. In particular, the memory model, which does not require cache coherency, allows almost anything to happen if you aren't extremely careful - and those problems are nearly impossible to test for, since many of the problems that could come up on some platforms, such as those without cache coherency, cannot come up on many development platforms.

I think the simile to C pointers is an apt one. To the extent that the primary benefit of Java is to remove "evil" undefined behavior such as that associated with wild pointers in C, Java threads are "evil". Sometimes they are a necessary evil, though.


I can't agree with this. Threads are well behaved except when you use them incorrectly. There by definition their behavior is non-deterministic. Evenstill you can calculate the realm of possible results if you really want to. The only thing random about thread behavior are those things the programmer leaves to be random.

Cache coherency and consistency must be guaranteed by the JVM according to the JLS, what the hardware does is a matter for the JVM writer to consider.
 
praveen kanakarajan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not a great expert in java.i have used threads in one of my project.i dont find any problems although its cautioned that you cannot rely it for a critical problem.i believe it all depends on how one uses it.If it is an evil thing then why sun would have included this feature.its only an warning thats all and we should consider it.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by praveen k:
im not a great expert in java.i have used threads in one of my project.i dont find any problems although its cautioned that you cannot rely it for a critical problem.i believe it all depends on how one uses it.If it is an evil thing then why sun would have included this feature.its only an warning thats all and we should consider it.


The warning would seem to indicate there is something special to be considered with threads that is not the case with the rest of Java. Which is wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!