• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Identifying instances

 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

We have an interesting design problem. We have a requirement where a state of object is controlled from different components of an application. Typically created by some component and destructed by some component.
(We need to start timer in some component, stop timer in some other component , calculate time elapsed and discard that timer)

We thought of using look up tables for this. However, there won't be any primary key to identify the specific instance of timer. (Same timer need to be stopped as the one which is intended)

Is there any better solution for this? Also is there any design pattern that addresses similar problem?

TIA,
Manohar
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you decide which component starts/stops which timer?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the rare case where everything is single threaded and symmetrical you can always guarantee timers will end in reverse order they were started and you can just use a stack of timers. I once had a PowerBuilder application where that actually worked ... for a while.

Otherwise I think the starter and stopper are going to need some common reference or key to match things up. It's nice if start() creates the id and stop() uses the same id, but it sounds like you can't pass something from the starter to the stopper. It would be easy enough to make up a string id or name for each timer, but in a multi-threaded or recursive situation that wouldn't be enough.

What all have you tried?
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preuss/James,

Thanks for your replies.

How do you decide which component starts/stops which timer?



I can answer one half of the question, i.e which component..I know from my requirements, which component can stop timer..Regarding "Which timer", this is the place where I am facing an issue. As my application is multithreaded, I cannot guarantee that I am going to stop intended timer.

use a stack of timers



As my application is multithreaded and need not be a symetrical one, I cannot use this approach.

I am interested in knowing any desing pattern address similar problem. I believe this is structural issue. I am planning to generate a unique key by some complex alogorithm.

Any better solutions?

TIA,
Manohar
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just got an idea..Can I use current threadID as a key?? Any issues in doing so? Though my application is multithreaded, it appears to me that single thread is gonna start and stop timers.

Any suggestions?

Thanks,
Manohar
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I'm not sure I understand what you are trying to do.

Do you have one component that is supposed to randomly stop one timer? Or one component per timer? Or what? ?
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok..I will try to give more detailed requirement..

We need to mesure time elapsed in backend which we are querying..(through scokets..)

There is a component which writes to the socket (say SocketWriter) and there is a component which listens socket (Say SocketListener). We start timer just before writing to socket in SocketWriter and stops timer in SocketListener and time elapsed will be time difference..

However issue is this is multithreaded program and simultaneously you can n 'timers' running..So stack based approach is ruled out..Secondly to identify these timers by generating some key, issue is SocketListener has no idea what key to use.

Hope I am clear..Let me know if you need more details on requirements.

TIA,
Manohar
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the sending and recieving sockets in the same JVM? Presumably they are because you reckon you can access the same timer object from both.

When you talk about "some key", I'm guessing this is so that you can send the key across the socket link. Likewise, I'm guessing that you either already have or plan to have a Map of key=>timer object available to both the sender and reciever.

If this is the case, all you need is some sort of unique key. There are plenty of unique key generators available, but something as simple as:



would probably do the job.

Each time you start a new Timer, add it to the map:



When you wish to stop a timer:



Of course, if only one activity occurs on each socket at a time, you could just use the socket itself as the key.

Or have I missed the point?
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My app is multi threaded..

For instance timer1, timer2, timer 3 are started in that order and timer3 may be required to be stopped before 1&2.

And my stopping component (SocketListener) have no clue regarding which timer to stop.. (Or which key to use to get timer)..

I am thinking of using thread ID as key. (Again assuming, that same thread will try to start and stop timers)

Anything wrong in assuming so?

Regards,
Manohar
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am curious what you need the timing information for? If all you are doing is trying to measure the performance of your system or something like that, it may be much simpler to use timestamped log messages for analysis. Is there something unique in the business logic, say like an order number, which you could use to match up the requests and responses from the backend?

Not sure if you already have a db in your system, but if you do, another approach is to insert timestamped events into the database (with rows of data something like UniqueID, TimeStamp, EventID), and then you can use simple queries to get your timing information.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manohar Karamballi:
There is a component which writes to the socket (say SocketWriter) and there is a component which listens socket (Say SocketListener). We start timer just before writing to socket in SocketWriter and stops timer in SocketListener and time elapsed will be time difference..
[/QB]



OK, so the SocketWriter needs to start the timer, and the SocketListener to stop it?

Then why don't you just give them both the same timer to work on?

Pseudocode:
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like my problem is not understood properly. Here is the code snippet where I am having issue.

public class BackendInvoker
{
public synchronized void writeToSocket(request)
{
//Write to socket logic
timer.start();
//Other business

}

public sychronized Response readFromScoket()
{
timer.stop() //Well..Issue is which timer to stop
//read from socket
}

}

Note that write and read methods are executed by different threads. write is executed when client requests. Where as read is executed from a thread which contantly polls sokcet for response.

Can anybody suggest the best way to handle this?

Also let me know if I am not clear in describing about problem?

Thanks in advance.
Manohar
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also my assumption that same thread is going to start and stop timers is incorrect.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many BackendInvoker instances do you have? Does one instance have to handle more than one timer?
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have pool of BackendInvoker instances which will be pooled. (Start size =2, incre =2 ; Max = 8). Potentially there can be 12 requests max at a time. And read thread polls for every 3 ms.

Please let me know if more information is required.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So one BackendInvoker handles more than one request at a time? And a timer is associated with a request?

How do you know which request a response is for?
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic