Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Timer and counters  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a file and have extracted the part of the file I am interested in, but now I'm struggling with performing some checks on the extracted data. My goal is to take the time in the last column from each line and decrement that time(I was thinking using counters) but my problem is I need to also keep track of the times in the first column on each line(but starting from 0.0, 0.1...) and initialise a new counter only when the time has reached that number. I am not sure how to keep track of the time that is in decimal? I was researching and I found I could potentially use Java Timer but I don't think I can use that to display numbers in milliseconds or use those times to perform calculations with them. Also, since I have both columns with times stored in ArrayLists of doubles, how would I even start counting from the first line, i.e. initialise a counter that doesn't start at 0, but at the desired time that is in the last column, say of the first line - 2.0?

How the file looks:
0.001: (some text)  2.0
0.001: (some text)  3.0
0.002: (some text)  ...

Thanks!
 
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a step back and tell us what you're REALLY trying to do. Getting timers to run at certain moments is not the goal, it's a possible way to achieve your goal.

I'm assuming that you're trying to show and hide subtitles at certain moments during playback of a video. If so, I take it that the first number is the time that the subtitle needs to be displayed, and the second number is the amount of time the subtitle needs to be displayed. Am I correct?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Take a step back and tell us what you're REALLY trying to do. Getting timers to run at certain moments is not the goal, it's a possible way to achieve your goal.

I'm assuming that you're trying to show and hide subtitles at certain moments during playback of a video. If so, I take it that the first number is the time that the subtitle needs to be displayed, and the second number is the amount of time the subtitle needs to be displayed. Am I correct?



Yes, you are actually right! But I also have the constraint that whilst a subtitle is displayed, another one cannot be displayed at the same time.
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so you can have input that conflicts with that constraint. You first need to ask yourself, what is the application required to do when such a conflict arises:

1) Keep displaying the current subtitle, and cut the next one short.
2) Cut the current subtitle short, and immediately start displaying the next subtitle.
3) Ignore one of the offending subtitles.
4) Something else?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Okay, so you can have input that conflicts with that constraint. You first need to ask yourself, what is the application required to do when such a conflict arises:

1) Keep displaying the current subtitle, and cut the next one short.
2) Cut the current subtitle short, and immediately start displaying the next subtitle.
3) Ignore one of the offending subtitles.
4) Something else?



Yes, that's fine but for now what I am interested in is finding a way how to initialise the counters while I'm looping through the ArrayList with all the durations and then at each point when a subtitle has to be shown, I decrease the counter. Once all counters are equal to 0 it means that there weren't any clashing i.e no subtitles shown at the same time.
 
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi  Trent,

so I guess you DO need to split the lines after 'Duration'.    

I don't think you need  more than one timer. You can always set a new delay after an event occurs (assuming you are using Swing). Also have a look at the class ScheduledThreadPoolExecutor. And maybe there are existing classes that do exactly what you need. No doubt specialists will reply.
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Trent Green wrote:Yes, that's fine but for now what I am interested in is finding a way how to initialise the counters while I'm looping through the ArrayList with all the durations and then at each point when a subtitle has to be shown, I decrease the counter.


You're jumping to implementation before you have your functional requirements down. The cleanest implementation often depends on your functional requirements.

You don't need any timers. First process your input so that you have a list of Subtitle objects that don't conflict with each other. Next, you schedule two tasks for each subtitle, one to display the subtitle and one to hide the subtitle.

You can schedule tasks with ScheduledExecutorService. I suggest you do something like this:
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly suggest that the thread pool you use for the scheduled executor service has only one thread. This eliminates race conditions, and will prevent subtitles from being hidden before they are shown.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Trent Green wrote:Yes, that's fine but for now what I am interested in is finding a way how to initialise the counters while I'm looping through the ArrayList with all the durations and then at each point when a subtitle has to be shown, I decrease the counter.


You're jumping to implementation before you have your functional requirements down. The cleanest implementation often depends on your functional requirements.

You don't need any timers. First process your input so that you have a list of Subtitle objects that don't conflict with each other. Next, you schedule two tasks for each subtitle, one to display the subtitle and one to hide the subtitle.

You can schedule tasks with ScheduledExecutorService. I suggest you do something like this:


Oh okay, that makes sense but before I do that I still need to firstly check if displaying a new subtitle is a valid action - by initialising a counter with the value of each duration and decrementing it and if at a particular time the counter for the first action is not 0 and there is another subtitle to be displayed, this is not a valid action. I just am unsure how to implement this really.
Also, I have already processed the input, but I just stored them in ArrayLists, so how would I store the different types of input as an object?(Sorry for the perhaps silly question)
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming you mean how to check that two subtitles don't overlap. You need to add a static Subtitle parse(String line) method to the Subtitle class that converts a line of input to a Subtitle instance. Then use that method to convert all the input to a list of subtitles. You can sort the list by "duration until the subtitles need to be displayed". Then you can easily check whether two subtitles overlap by checking whether the hiding time of one subtitle is after the showing time of the next.

Try to come up with a solution, add if you're stuck show us your code and tell us what's wrong.
 
Piet Souris
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But can you delay a subtitle, if its predecessor is still busy? Don't you face the risk of getting completely out of sync?
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's why I said that the functional requirements first need to be specified.
 
Piet Souris
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is why I thought about a simple Timer, where you simply adjust the delay after each event, cutting the present subtitle if necessary (a SwingWorker to handle this seems fine yo me too). Or better, indeed process the inputlines such that no end time lies after the begin time of its successor. But you already described that scheme, if I understood it correctly, and if so, I agree. But you are right: without knowing the specs it is only guessing.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The counters and timers are hardware component of microcontroller, which is used in many applications for providing the precious time delay with count pulses. The counter and timer both are implemented by using the software technique.
 
Saloon Keeper
Posts: 4413
108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sonai kale wrote:The counters and timers are hardware component of microcontroller, which is used in many applications for providing the precious time delay with count pulses. The counter and timer both are implemented by using the software technique.


The question is about software, not hardware. As an aside, it makes no sense to say "hardware component ... implemented using software". Please make sure your posts help in answering the question.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I'm assuming you mean how to check that two subtitles don't overlap. You need to add a static Subtitle parse(String line) method to the Subtitle class that converts a line of input to a Subtitle instance. Then use that method to convert all the input to a list of subtitles. You can sort the list by "duration until the subtitles need to be displayed". Then you can easily check whether two subtitles overlap by checking whether the hiding time of one subtitle is after the showing time of the next.

Try to come up with a solution, add if you're stuck show us your code and tell us what's wrong.



I still want to use my idea about counters though but I've been struggling to wrap my head around it all day. Basically I was thinking - what do you think about the following idea - I use just a variable time in milliseconds and then just increment it with 0.1 sequentially. But as far as counters are concerned - I still can't figure out how I could initialise numerous counters for each value in the arrayList while I'm looping through the ArrayList.

Piet Souris wrote:But can you delay a subtitle, if its predecessor is still busy? Don't you face the risk of getting completely out of sync?


I just want to ignore a subtitle if a predecessor is still busy but still my prerequisite before I start showing and hiding subtitles is to check that a successor subtitle is not overlapping with the previous one.
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I still want to use my idea about counters though but I've been struggling to wrap my head around it all day.


That's because doing this with counters is very contrived and awkward. Java already has easy to use classes that handle calculations with durations of time. I can't stop you if you want to do it your way, but it's not object oriented programming. I suggest you first focus on writing code that just reads the input to objects you can work with, and *then* worry about what to do with them.
 
Piet Souris
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming this topic is sequel to your previous topic "String split", if we have these lines:
0.001: (some text)  2.0
0.001: (some text)  3.0
0.002: (some text)  ...

What is the format of 0.001? seconds.milliseconds? And is say 0.002 the time difference from the start, or from the end of the previous line?
And what is the format of the final 2.0, 3.0 and how is this to be recognized? Like: the text after the last space?
Can you supply some 9full) demo lines?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Assuming this topic is sequel to your previous topic "String split", if we have these lines:
0.001: (some text)  2.0
0.001: (some text)  3.0
0.002: (some text)  ...

What is the format of 0.001? seconds.milliseconds? And is say 0.002 the time difference from the start, or from the end of the previous line?
And what is the format of the final 2.0, 3.0 and how is this to be recognized? Like: the text after the last space?
Can you supply some 9full) demo lines?



It is in fact a sequel.
Basically, what I posted was just an example, not a correct sequence of lines. This is an example of correct times where there's no overlapping.
0.000: (some text)   0.001
0.001: (some text)   0.001
0.002: (some text)   0.001
0.003: (some text)   0.001

Both first and last columns' numbers are in milliseconds. 0.002 is simply the time that the current line starts.
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet means an actual real snippet of input. (Some text) is obviously not real input. We don't know if there are useful characters in the input that can be used to separate the text from the duration at the end of the line. If there are, you can use a Scanner to parse data from your input. If there aren't, I think a regular expression is the best way to go. It would look like this:
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Piet means an actual real snippet of input. (Some text) is obviously not real input. We don't know if there are useful characters in the input that can be used to separate the text from the duration at the end of the line. If there are, you can use a Scanner to parse data from your input. If there aren't, I think a regular expression is the best way to go. It would look like this:


Do you mean to extract the last column numbers? I've already extracted the numbers with start and duration times respectively. What I've been trying to figure out is the algorithm on how to check if a subtitle can be displayed at a particular time. Basically now I've been thinking of doing that in the following way - take the start time + duration time of the line and if that sum is less than the start time of the next line, the second line subtitle can be displayed? What do you guys think about that?
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what I would do.
 
Piet Souris
Master Rancher
Posts: 2539
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and it is possible that some lines will get skipped. Compare it to a given set of intervals, that must be adjusted such that no two intervals overlap.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That is exactly what I would do.



Piet Souris wrote:Yes, and it is possible that some lines will get skipped. Compare it to a given set of intervals, that must be adjusted such that no two intervals overlap.



Okay, now that I've got this figured out, I realised I'm not sure how I would code that that action(i.e. of showing a subtitle) is valid? Any hints are appreciated!
 
Stephan van Hulst
Saloon Keeper
Posts: 8728
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate over the sorted list of subtitles, keeping track of the previous subtitle you visited. For each subtitle, compare its starting time to the ending time of the previous subtitle. If it's smaller, it's invalid, and it must either be skipped, cut short, or delayed.

I just gave you the recipe for some code. Show us how far you got with it, and we'll give you pointers when you're stuck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!