Java Threading

2,570 views

Published on

A basic explanation of Java threads

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,570
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
191
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Java Threading

  1. 1. Guillermo Schwarz Sun Certified Enterprise Architect
  2. 2. What are threads? <ul><li>Each thread is like an operating system process, except that all threads in a process share the memory. </li></ul><ul><li>Threads execute code concurrently and therefore must carefully control access to shared resources. </li></ul><ul><ul><li>Otherwise data can become inconsistent. </li></ul></ul>
  3. 3. All memory is shared? <ul><li>No, only globals and heap. </li></ul><ul><li>In Java, classes are global and static variables declared inside classes are global too. All the rest is local. </li></ul><ul><li>All variables declared inside a method are locals, therefore they are not shared. </li></ul><ul><li>The heap memory is never a problem even though it is shared, because variables pointing to them are either global or local. </li></ul>
  4. 4. Visually Thread-1 Thread-2 Thread-3 Stack-1 Stack-2 Stack-3 Globals Heap
  5. 5. Visually <ul><li>class Example </li></ul><ul><li>{ </li></ul><ul><ul><li>static String salutation = “hello”; // to be protected </li></ul></ul><ul><ul><li>String person = “world”; </li></ul></ul><ul><ul><li>public void concat( char c ) // offending method </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> String aux = “$” + c + “$”; </li></ul></ul><ul><ul><li> synchronized ( salutation ) // protection </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><ul><li> salutation += aux; // potential offense </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>person += aux; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  6. 6. Java Threads <ul><li>Originally Java threads were green threads, meaning the JVM simulated threads using only one operating system thread. </li></ul><ul><li>Since Java 2, threads are implemented using operating system threads, meaning when multiple processors are available, they actually execute in parallel. </li></ul><ul><li>Green and OS threads can have the same problems when used carelessly. </li></ul>
  7. 7. J2EE <ul><li>The J2EE standard recommends not to use synchronization: </li></ul><ul><ul><li>The rationale is that synchronization is too difficult to get right (dead locks, race conditions, contention, convoy formations, etc.). </li></ul></ul><ul><ul><li>This also means you can’t use static variables unless you make them final (i.e.: you assign them only once). </li></ul></ul><ul><ul><li>In J2EE data resides in the database, where it belongs, databases use database transactions and that protects the integrity of data . </li></ul></ul>
  8. 8. J2EE <ul><li>In practice, it is ok to synchronize on static variables as long as: </li></ul><ul><ul><li>Changes are consistent once they leave the synchronized block. </li></ul></ul><ul><ul><li>They don’t involve database changes (use database transactions for that) or any other non memory change, as for example files, sockets, etc. </li></ul></ul><ul><ul><li>They either change one variable or they involve only one lock, or if they change more than one variable and involve more than one lock, they are made in a manner that does not produce deadlocks, live locks, etc. </li></ul></ul>
  9. 9. Problems with Synchronization <ul><li>Forget to synchronize some global memory => race problems. </li></ul><ul><li>Too much synchronization => dead lock and live lock (contention). </li></ul><ul><li>Synchronizing little blocks of code instead of meaningful chunks => contention + race problems. </li></ul><ul><li>Synchronizing too big chunks of code => contention. </li></ul>
  10. 10. Synchronization Mechanisms <ul><li>The synchronized keyword. </li></ul><ul><li>Read/write locks built on top of the synchronized keyword: </li></ul><ul><ul><li>Useful for read dominated data. </li></ul></ul><ul><ul><li>Part of Java 5. </li></ul></ul><ul><li>It is even better to use non blocking data structures (no contention). </li></ul>
  11. 11. What about other shared resources? <ul><li>The synchronized keyword is not meant to be used with other resources except memory. </li></ul><ul><li>Using synchronized to access files, for example, has no defined effect. May work on some technology stacks (JVM + operating system), may fail in others or may produce hard to track and unpredictable side effects in all of them. </li></ul>
  12. 12. J2EE <ul><li>J2EE recommends not using threads at the application level, but leaving it up to the application server. </li></ul><ul><li>Spring Framework author, Rod Johnson, prefers that developers use the thread API. (1) </li></ul><ul><li>Nevertheless, Rod suggest that developers should not use JDBC directly, because it is easy to mess up. (2) </li></ul><ul><li>JDBC is way too easy compared to the thread API. </li></ul><ul><li>(1): See J2EE Development Without EJB, Rod Johson et al., page 344. </li></ul><ul><li>(2): See same book, on page 145 under “JDBC support”. </li></ul>
  13. 13. The Real Problem <ul><li>Why do we want to use the thread API? </li></ul><ul><ul><li>Is it liveliness? </li></ul></ul><ul><ul><ul><li>The producer consumer design pattern is more lively and the thread API can be hidden behind the pattern for application programmers to use. </li></ul></ul></ul><ul><ul><li>Is it better throughput? </li></ul></ul><ul><ul><ul><li>Again, the producer consumer pattern is better. </li></ul></ul></ul><ul><ul><li>Is it handling more concurrent users? </li></ul></ul><ul><ul><ul><li>That is a good one, but any servlet conatiner already handles it the best way. </li></ul></ul></ul><ul><ul><ul><li>Do not store information in the session for best results. </li></ul></ul></ul>
  14. 14. Summarizing <ul><li>Threads should be hidden in low level libraries. </li></ul><ul><li>In applications, only globals (static variables) must be synchronized. </li></ul><ul><li>The shorter the span they are synchronized, the better (non blocking being the best). </li></ul><ul><li>Using the database is even better, since it already has “serialization” built-in under a mechanism called “transaction”, meaning that even thought everything occurs simultaneously, the end result is as if every thread executed one after the other. </li></ul>
  15. 15. Guillermo Schwarz Sun Certified Enterprise Architect

×