• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Queuing

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the below program:
/***********************************************************/
import java.io.*;
import java.util.*;
public class LogTest extends Thread {
Vector QueList = new Vector();

public LogTest() {
//
}

private void writeMessages() {
System.out.println("Write Messages");
try{
FileOutputStream out = new FileOutputStream("test.log", true);
if (!QueList.isEmpty()) {
int rowCount = QueList.size();
for (int i=0; i< rowCount;i++) {
out.write(QueList.elementAt(i).toString().getBytes());
}
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public void addMessage () {
for (int i=0; i < 400000; i++) {
QueList.addElement("Hello World.. Peace o Earth...");
}
}

public static void main (String args[]) {
LogTest Test = new LogTest();
Test.start();
Test.addMessage();

}
public void run() {
try {
while (true) {
writeMessages();
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/***********************************************************/
I need your help here.
Every second or so the threads wakes up and writes everything in Quelist vector to Test.log file. During this process my understanding is it is going to lock the Quelist vector not allowing addmessage method to write any messages to it. I would like to have an implementation where when committing the messages to file i would like the program continue to write to the vector. Sort of queuing...
One thing i thought about doing is cloning the vector in write messages method and clearing the quelist vector and unlock the vector... but I am not sure whether it is a good idea. Because cloning will probably take some time as well...
Any help is appreciated...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's my two cents but I may be missing some detail about your application...
I think I understand what you're doing I'd do it a slightly differnt way:
use whats known as a worker queue... something like this:

when a thread calls dequeue (your file writing thread) it will be blocked until there is an element in the queue. when a thread calls enqueue (The thread that is dumping the data) it will place the data (a fast opperation) and leave.
I hope this helps..sorry if it is confusing
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!