Re: [Threading to manage simulated printing jobs]
On Sep 9, 11:07 am, getsanjay.sha...@gmail.com wrote:
Hello to all Javascript programmers out there. :)
I am really ashamed to say that even after 6 months of intermittent
Java programming I have been a complete failure at grasping the
concepts or the real thing behind 'threading'. My mind just seems to
go in a state of deadlock and I keep hours looking at the screen
trying to come up with a design to get the problem solved using
threads.
Many people have a hard time grasping threads, especially if they try
to understand it without being taught exactly how they work. I
suggest reading about it, see below.
I am OK with normal Java programs. Also here in my case I
have not used the convenience classes provided by java so that I can
get my concepts right.
Here I have written a program which simulates a printing job in which
'Consumer' is a printing device or software and 'Producer' submits a
printing job. But the output I get is highly deterministic i.e. the
same everytime I don't even know if I have got it right or wrong. Some
comments / pointers / alternate designs / tips / revelations would be
greatly appreciated.
[snip code]
The best pointer I can give you: Read the book "Java Concurrency In
Practice" <http://www.javaconcurrencyinpractice.com/>. It is a great
book that explains the ins and outs of multi-threaded programming in
Java. After you read it, not only will you be able to diagnose the
problems with your code, you'll be able to design it in a much more
robust manor.
What you need to learn about is synchronization and ordering. Unless
you synchronize your threads some way (either by using synchronize
*appropriately*), they may interact in unexpected ways. Don't just
add synchronization willy-nilly either, it is possible to figure out
where you need to sync to achieve thread safety. All that information
and more is in the book I recommended.
Good luck,
Daniel.