• Post Reply Bookmark Topic Watch Topic
  • New Topic

dynamically adding to queue  RSS feed

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am running fallowing programme
from java almanac


class Worker extends Thread {
// Special end-of-stream marker. If a worker retrieves
// an Integer that equals this marker, the worker will terminate.
static final Object NO_MORE_WORK = new Object();

WorkQueue q;

Worker(WorkQueue q) {
this.q = q;
}
public void run() {
try {
while (true) {
// Retrieve some work; block if the queue is empty
Object x = q.getWork();

// Terminate if the end-of-stream marker was retrieved
if (x == NO_MORE_WORK) {
break;
}

// Compute the square of x
// int y = ((Integer)x).intValue() * ((Integer)x).intValue();
String test = x.toString();
Thread.currentThread().sleep( 500);
System.out.println(" value"+ test);

}
} catch (InterruptedException e) {
}
}
}


import java.io.*;
import java.util.*;

public class WorkQueue {
LinkedList queue = new LinkedList();

// Add work to the work queue
public synchronized void addWork(Object o) {
System.out.println("inside addworkqueue size is"+queue.size());

queue.addLast(o);
notify();
}

// Retrieve work from the work queue; block if the queue is empty
public synchronized Object getWork() throws InterruptedException {
while (queue.isEmpty()) {
wait();
}
// queue.wait(1000);
return queue.removeFirst();
}
}


import java.io.*;
import java.util.*;
public class threadtest
{



public static void main(String[] args)
{
System.out.println("Hello World!");
// Create the work queue
WorkQueue queue = new WorkQueue();
threadtest jobtest = new threadtest();
// Create a set of worker threads
final int numWorkers = 1;
Worker[] workers = new Worker[numWorkers];
for (int i=0; i<workers.length; i++) {
System.out.println("step 1");
workers[i] = new Worker(queue);
workers[i].start();
}

// Add some work to the queue; block if the queue is full.
// Note that null cannot be added to a blocking queue.
for (int i=0; i<10; i++) {
// System.out.println("step 2");

queue.addWork("Hello World1");
}

// Add special end-of-stream markers to terminate the workers
for (int i=0; i<workers.length; i++) {
queue.addWork(Worker.NO_MORE_WORK);
}

}





}
}

how to addWork("sample");

to WorkQueue while the threadtest is running ?
while the above programme out puts hello world .. while this is running i want to add sample and the out put must add sample after helloworld

thanks in advance
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to add more tasks in between the loop where you load up "hello" and the loop where you add NO_MORE_WORK. Have you tried that? I like the NO_MORE_WORK object to shut down a worker thread. That's nice.

When posting code, use the "CODE" button below the editor then insert your code between the open & close tags. That will preserve your indentation and make things a lot easier to read.

This is a good thread exercise and looks like it's going nicely. Let us know how it works out!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!