Solved

Queue and singleton in Java

Posted on 2011-09-10
8
458 Views
Last Modified: 2012-08-13
Hi,

I want to implement a singleton queue class, which will be accessed by multiple threads.

The queue contains no more than 10000 of files. Most likely files with same prefix will be put together in the queue, but not guaranteed. I would like to have multiple thread read through the queue, identify a group of files of that kind.

I heard thread-safe is difficult to implement for queue, and would like some experts' views, suggstions and for the best, some code.

Thanks
0
Comment
Question by:wsyy
  • 4
  • 4
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36518166
If you have different threads only reading the queue and not changing it - there may be no issue  with  thread-safety
0
 

Author Comment

by:wsyy
ID: 36518184
how to implement it though, for_yan. do you have some code?

I forgot to mention that after reading and grouping such files, they should be taken off the queue. will that be considered challenging?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36518204

If you are going to have threads some of which will be populating and others removing
data form the queue, then you probably want to use thee BlockingQueue.
Read the good article with some code examples here:

http://www.developer.com/java/ent/article.php/3645111/Java-5s-BlockingQueue.htm
0
 

Author Comment

by:wsyy
ID: 36518787
for_yan, thanks for the link.

I need to put in the BlockingQueue the files to be processed. The putting process is not multi-threading.

I also need to get the files off the queue, which however is multi-threading.

How can I modify the cited code example to achieve my goals?

In the example, I only see in the Client.java how to submit a request to the Server.java. I nonetheless see a process to identify, group, and do other operation in the queued requests.

Please help.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:wsyy
ID: 36518806
for_yan,

Do I need to develop two kinds of requests?

One is the putting request (or the producer request), and the other is the getting request (or the consumer request).

The cited example seems to not distinguish the two requests.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519296
BlockingQueue has different options for your possible needs  - method put will be blocking till the space in
the BlockingQueue becomes avaialabe - this is for queues with limited number of entries.
If you don't have limit on the number of entries in the queue you may use method offer which
will add element anyway.
Method take() will be blocking until the elements arrived if there is nothing in the BlockingQueue object - I
think you need to use this method.

As I understand you need to put file names to BlockingQueue not the files themselves.
There is also Iterator method which will not affect the contents of the queue but whih would allow you to analyze the contetnts
of your queue but does not affect the contents of it say before you decide what to do with the elements of this queue
0
 

Author Comment

by:wsyy
ID: 36519358
for_yan:

How can I start the server at the very beginning so that all the clients can call it and don't have to regenerate a new server instance?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519391
This is separate issue specific to your situation - usually there is no issue in generating a new
server instance for eachj clinet - this server instance is just a handle to the server and if the clients are separate,
and say running on different machines they may each need such seprate handle.
Say, if you are running RMI server (Remote Method Invoication) server then you start server on one machine - if you have clinest running
in separate JVM (usually even on different computers) then each clinet will lookup the server through the registry get the handle to that server instance and use it
to invoike the method on the server. If you want to run several methods from withi one java application you can
have a statoc variable for the server instance and use one server instance to invoke all methods in that application,
but if each one uses it own instance it is also not a big deal.
So how you set up interactions between your clients and the server - this is a separate issue.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now