473,480 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Are there any flaws in this design

2 New Member
Hi,

I've designed this small application. I want to know if there are some chances of race conditions, deadlocks or memory leaks in it. Please give your comments.

The problem is that I need to do some task that takes about 3 minutes to complete. The same task can be submitted by multiple clients. So while a task is running, if another request comes for processing the same task again, I don't want to do it again, and I want to use the results produced by already running task.

Here are the classes involved:

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  *This class accepts the queries from the client
  3.  */
  4. public class Processor {
  5.  
  6.     public List<Details> processQuery(Query query) throws ProcessorException {
  7.  
  8.     QueryResult result = requestCache.getResult(query);
  9.  
  10.     synchronized (result) {
  11.  
  12.         while (!result.isCompleted() && result.isClean()) {
  13.                 // task is being processed, so wait
  14.         try {
  15.             result.wait();
  16.         } catch (InterruptedException e) {
  17.             //TODO:
  18.         }
  19.         }
  20.     }
  21.         // wait over 
  22.     if (result.isClean() && result.isCompleted()) {
  23.         return result.getList();
  24.     }
  25.  
  26.         // task was not processed successfully
  27.     throw new ProcessorException();
  28.     }
  29.  
  30. }
  31.  
  32. /*
  33.  *This is the result class whose objects will hold results of the processed tasks
  34.  */
  35. public class QueryResult {
  36.     private volatile boolean completed = false;
  37.  
  38.     private volatile boolean clean = true;
  39.  
  40.     private List<Details> list;
  41.  
  42.     public List<Details> getList() {
  43.     return list;
  44.     }
  45.  
  46.     public synchronized void setList(List<Details> list) {
  47.     this.list = list;
  48.  
  49.     this.completed = true;
  50.  
  51.         // notify all threads waiting for the results
  52.     notifyAll();
  53.     }
  54.  
  55.     public boolean isCompleted() {
  56.     return completed;
  57.     }
  58.  
  59.     public boolean isClean() {
  60.     return clean;
  61.     }
  62.  
  63.     // something has gone wrong, set isClean flag and notify all waiting threads
  64.     public synchronized void corrupt() {
  65.     this.clean = false;
  66.  
  67.     notifyAll();
  68.     }
  69. }
  70.  
  71. /*
  72.  *This class does the actual processing and maintains the list of tasks being processed
  73.  */
  74. public class RequestCache {
  75.  
  76.     private final ConcurrentHashMap<Query, QueryResult> results = new ConcurrentHashMap<Query, QueryResult>();
  77.  
  78.     public QueryResult getResult(Query query) {
  79.  
  80.     QueryResult result = results.get(query);
  81.  
  82.     if (result != null) {
  83.             // query is already under processing
  84.         return result;
  85.     }
  86.  
  87.     result = new QueryResult();
  88.  
  89.     QueryResult previousResult = results.putIfAbsent(query, result);
  90.  
  91.     if (previousResult != null) {
  92.             // some other thread already submitted the query for processing
  93.         return previousResult;
  94.     }
  95.  
  96.         // now we need to process the query ourself
  97.     try {
  98.             List<Details> list = doProcessing(query); // this takes about 3 minutes
  99.  
  100.             // this will set the list as well as notify all threads waiting for this result
  101.         result.setList(list);
  102.     } catch (Exception e) {
  103.         // an exception occured, so we should notify everybody of the error
  104.         // remove the query from the map, so that new requests does not get the corrupt response
  105.         results.remove(query);
  106.  
  107.         result.corrupt();
  108.         synchronized (result) {
  109.         result.notifyAll();
  110.         }
  111.  
  112.     }
  113.  
  114.     results.remove(query);
  115.  
  116.     return result;
  117.     }
  118.  
  119. }
  120.  
Jun 7 '07 #1
0 900

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2531
by: Nikola Skoric | last post by:
I've recently seen a phpbb forum hacked. The hacker removed all accounts, formed only one admin acc under his control and removed all articles. How did he do that, anyway? And how can I protect my...
4
1916
by: dave | last post by:
One thing i'm finding very quickly as my servlets become more logically condensed, is that an "object" in the sense of "object oriented design" becomes restricted to a "webpage/servlet". How do you...
98
6179
by: Pamel | last post by:
I know this must have been asked elsewhere, but I cannot find it. There is a piece of text on my web page that I don't want browsers to resize. IE won't resize it if I specify the size in px, but...
0
1154
by: mike | last post by:
regards: what is the goodness and flaws of following programming architecture?....@@. http://www.wretch.cc/album/show.php?i=otp&b=1&f=1107098054&p=0 Any positive suggestion is welcome....
20
1609
by: Allen Browne | last post by:
To help you avoid some of the more common and long-standing bugs in Access, the page: http://allenbrowne.com/tips.html contains a new section entitled, "Flaws in Access". The section currently...
16
2297
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I...
9
1479
by: tnspc | last post by:
I'm a complete newbie when it comes to SQL, and my tables have a serious flaw that I've been unable to correct (unless the fault lies in my queries alone). Here's the code for my tables: drop...
19
3131
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
4869
Plater
by: Plater | last post by:
PSA: The service "Application Layer Gateway Service" (alg.exe) is a big sham. It was configured to auto-start by default. The internet says "don't stop this or your internet won't work" well I...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.