G
getsanjay.sharma
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. 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.
import java.sql.Timestamp;
import java.util.*;
public class ThreadTime {
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
PrintQueue queue = new PrintQueue();
Producer producer = null;
for (int i = 0; i < 100; ++i) {
producer = new Producer(queue, "c:/" + i + ".txt");
}
new Consumer(queue);
new Consumer(queue);
new Consumer(queue);
}
}
class Consumer {
private static int gId;
private PrintQueue queue;
private String id;
private 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();
}
}
};
public Consumer(PrintQueue queue) throws Exception {
this.queue = queue;
this.id = "Consumer" + gId;
gId++;
Thread t = new Thread(job);
t.join(); /* Does this even do anything? */
t.start();
}
}
class Producer {
private PrintQueue queue;
private String file;
public Producer(PrintQueue queue, String file) throws Exception {
this.queue = queue;
this.file = file;
Thread t = new Thread(job);
t.join(); /* Does this even do anything? */
t.start();
}
private Runnable job = new Runnable() {
public void run() {
try {
//System.out.println("Inside run of producer");
Thread.sleep(500);
queue.putJob(new PrintJob(file, new Timestamp(new Date()
.getTime())));
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
class PrintQueue {
public List<PrintJob> queue;
public static int jobId;
public PrintQueue() {
queue = new LinkedList<PrintJob>();
}
public synchronized PrintJob getJob() {
if (queue.size() != 0)
return (queue.remove(0));
else
return (null);
}
public synchronized void putJob(PrintJob job) {
queue.add(job);
}
}
class PrintJob {
private static int jobId;
public int id;
public Timestamp time;
public String path;
public PrintJob(String path, Timestamp time) {
this.id = jobId;
this.path = path;
this.time = time;
jobId++;
}
private PrintJob() {
}
public String print() {
return (this.toString());
}
public String toString() {
return (" Id: " + id + " Path: " + path + " Time: " + time);
}
}
Thanks a lot for your valuable time.
Regards,
S T S
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. 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.
import java.sql.Timestamp;
import java.util.*;
public class ThreadTime {
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
PrintQueue queue = new PrintQueue();
Producer producer = null;
for (int i = 0; i < 100; ++i) {
producer = new Producer(queue, "c:/" + i + ".txt");
}
new Consumer(queue);
new Consumer(queue);
new Consumer(queue);
}
}
class Consumer {
private static int gId;
private PrintQueue queue;
private String id;
private 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();
}
}
};
public Consumer(PrintQueue queue) throws Exception {
this.queue = queue;
this.id = "Consumer" + gId;
gId++;
Thread t = new Thread(job);
t.join(); /* Does this even do anything? */
t.start();
}
}
class Producer {
private PrintQueue queue;
private String file;
public Producer(PrintQueue queue, String file) throws Exception {
this.queue = queue;
this.file = file;
Thread t = new Thread(job);
t.join(); /* Does this even do anything? */
t.start();
}
private Runnable job = new Runnable() {
public void run() {
try {
//System.out.println("Inside run of producer");
Thread.sleep(500);
queue.putJob(new PrintJob(file, new Timestamp(new Date()
.getTime())));
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
class PrintQueue {
public List<PrintJob> queue;
public static int jobId;
public PrintQueue() {
queue = new LinkedList<PrintJob>();
}
public synchronized PrintJob getJob() {
if (queue.size() != 0)
return (queue.remove(0));
else
return (null);
}
public synchronized void putJob(PrintJob job) {
queue.add(job);
}
}
class PrintJob {
private static int jobId;
public int id;
public Timestamp time;
public String path;
public PrintJob(String path, Timestamp time) {
this.id = jobId;
this.path = path;
this.time = time;
jobId++;
}
private PrintJob() {
}
public String print() {
return (this.toString());
}
public String toString() {
return (" Id: " + id + " Path: " + path + " Time: " + time);
}
}
Thanks a lot for your valuable time.
Regards,
S T S