[Source] Extremely Simple Timer.

by Security. - 02-10-2015, 06:47 AM
Closed Account
Posts:
3
Joined:
Oct 2015
Likes:
0
Credits:
5
Reputation:
0
2 Years of Service
#1
OP
Posted: 02-10-2015, 06:47 AM
Code:
public class Timer {

private long time;

public boolean wait(final int ms) {
return System.currentTimeMillis() - this.time >= ms;
}

public void reset() {
this.time = System.currentTimeMillis();
}

}

This is a simple timer that I have wrote. It waits a specific amount of milliseconds before executing code. Here is an example of this timer in use:
Code:
final Timer timer = new Timer();
while (true) {
if(timer.wait(2000)){
System.out.println("Hello, World!");
timer.reset();
}
}
If the above code is implemented properly, the characters "Hello, World!" will be printed to the standard output system every two seconds (two thousand milliseconds).

Using a timer can be extremely helpful, and sometimes even necessary. For example, if you want to create something like a spammer program, there are times where you'll need a delay. If you're creating a spammer for a computer game, there are usually things to stop you from sending too many messages at once. Therefore, you'll need a delay to comply with it.
y u so shit
Posts:
6,599
Joined:
Feb 2015
Likes:
143
Credits:
5,879
Reputation:
93
3 Years of Service
#2
Posted: 11-10-2015, 10:01 AM
Great share, JAVA is pretty un-complicated once you've gotten used to it. We used to need timers for programs we made in College, however we had a different variation on this!

I'm sure this will help people learn JAVA or at least get them on the road to learning it.
The Pengest Turkey

[Image: The-Chicken-Connoisseur-PENGEST-TURKEY-1-1.gif]

أ) هو ميمي)
Supreme
Posts:
1,988
Joined:
Jan 2015
Likes:
115
Credits:
444
Reputation:
91
3 Years of Service
#3
Posted: 12-10-2015, 08:04 PM
(11-10-2015, 10:01 AM)Harry Wrote: Great share, JAVA is pretty un-complicated once you've gotten used to it. We used to need timers for programs we made in College, however we had a different variation on this!

I'm sure this will help people learn JAVA or at least get them on the road to learning it.

Yeah, I had to do a controlled assessment on it for my GCSE. For me it was piss easy.
xxx
Lurker
Posts:
2
Joined:
Feb 2016
Likes:
0
Credits:
2
Reputation:
0
2 Years of Service
#4
Posted: 09-02-2016, 01:23 PM
great i am a beginner learning java but i am a master of C and C++
Newbie
Posts:
15
Joined:
Feb 2016
Likes:
0
Credits:
16
Reputation:
0
2 Years of Service
#5
Posted: 16-02-2016, 06:23 PM
Nice share even though it's extremely simple.
Lurker
Posts:
8
Joined:
Feb 2017
Likes:
0
Credits:
8
Reputation:
0
1 Year of Service
#6
Posted: 26-02-2017, 07:38 AM
Nice practice, very easy to read the code.
Novice
Posts:
35
Joined:
Jul 2017
Likes:
0
Credits:
1
Reputation:
0
1/2 Year of Service
#7
Posted: 02-07-2017, 04:41 PM
i am so new to java . this is really awesome ill try compiling this .
Junior Member
Posts:
56
Joined:
Jul 2017
Likes:
1
Credits:
10
Reputation:
0
1/2 Year of Service
#8
Posted: 13-07-2017, 02:44 AM
Compiling this, im not new to java but i skimmed through the code and it looked fine.
Novice
Posts:
21
Joined:
Jul 2017
Likes:
0
Credits:
0
Reputation:
0
1/2 Year of Service
#9
Posted: 30-07-2017, 01:44 AM
That's awesome! One thing about it though, is that while it's waiting on the timer to be ready, it's constantly running through the while loop! If you want to ensure that the CPU isn't spending all its time iterating through that loop, you might want to put a small rest time in there, something like Thread.sleep(5) which won't really be noticed unless your project requires very precise timing.

Great job so far though!
The last reply on this thread is older than a month. Please do not unnecessarily bump it.
Register an account or login to reply
Create an account
Create a free account today and start posting right away. It only takes a few seconds.
Login
Log into an existing account.
1 Guest(s)