• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

dynamically adding to queue

Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {

// 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());


// Retrieve work from the work queue; block if the queue is empty
public synchronized Object getWork() throws InterruptedException {
while (queue.isEmpty()) {
// 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);

// 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++) {



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
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic