Vortexmind: free your mind Tu ne cede malis sed contra audentior ito

10Oct/061

Java Threads – Runnable – Issues – Newbie Tips

Since I'm getting back on programming, I'm currently re-skilling myself on this duty. Having not programmed Java in a while, I've found myself on trouble on this simple topic: Java concurrency and threads. I will insert on the blog some "quick tutorials" about various subjects. I've found that programmers sometimes need a fast solution on a given problem. They could surely get it by looking at the API's and so on, but it costs valuable time. So I will go straight to the problem.

  • Problem: I need to do a fixed job on some data in a parallel fashion.
  • Solution: using a multi-threaded model, I have two classes Controller and Worker. Worker is essentially a thread that does the fixed job and reports the data to the Controller
  • Main issues: is it better to "extends Thread" or to" implements Runnable"? How to accessing shared variables concurrently and avoid race conditions? How to pass data from Worker to Controller?
Print this post Print this post
   
Creative Commons Licence
This work by Paolo Tagliaferri is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.