Results 1 to 2 of 2
Thread: Stopwatch not working.
- 07-24-2012, 08:07 PM #1
Stopwatch not working.
I have created a stop watch, and it is not working.
The stop watch format is hh:mm:ss:mm
However, it always display as 0:0:0:1 no matter how long it is active.
I have added a system.out.println debug at line:50, and it seems like the code is only executed once.Java Code:package clock.core; enum Direction { POSETIVE, NEGATIVE; } public class Clock { public static void main (String... args) throws InterruptedException { Clock c = new Clock(); c.start(); Thread.sleep(25); c.stop(); System.out.println(c.toString()); } private int hour,minute,second,milli; private boolean running, posetive, stop; private Direction direction; private Thread clock; private TimerThread counterThread; private transient int debugCounter = 0; public Clock () {} public void start () { if (running) throw new IllegalStateException ("Can not start a clock that is already running."); init (); clock = new Thread () { @Override public void run () { int currentTime = 0; counterThread = new TimerThread (Clock.this); counterThread.start(); while (!stop) { if (counterThread.millisecond <= currentTime) continue; else currentTime = counterThread.millisecond; if (posetive && direction == Direction.POSETIVE || !posetive && direction == Direction.NEGATIVE) increment (); else if (posetive && direction == Direction.NEGATIVE) { decrement (); if (hour == 0 && minute == 0 && second == 0 && milli == 0) posetive = false; } else if (!posetive && direction == Direction.POSETIVE) { decrement (); if (hour == 0 && minute == 0 && second == 0 && milli == 0) posetive = true; } } } }; clock.start(); running = true; } public void stop () { stop = true; running = false; } public void shift () { direction = (direction == Direction.POSETIVE) ? Direction.NEGATIVE : Direction.POSETIVE; } @Override public String toString () { StringBuilder time = new StringBuilder (13); if (!posetive) time.append("-"); time.append(hour).append(":").append(minute).append(":").append(second).append(":").append(milli); return time.toString(); } private void increment () { if (++milli == 999) { milli = 0; if (++second == 59 ) { second = 0; if (++minute > 59) { minute = 0; hour++; } } } } private void decrement () { if (--milli == 0) { milli = 999; if (--second == 0) { second = 59; if (--minute == 0) { minute = 59; hour--; } } } } private void init () { hour = 0; minute = 0; second = 0; milli = 0; running = false; posetive = true; stop = false; direction = Direction.POSETIVE; } private static class TimerThread extends Thread { int millisecond; Clock clock; TimerThread (Clock clock) { this.clock = clock; millisecond = 0; } @Override public void run () { try { Thread.sleep(1); } catch (InterruptedException e) {} millisecond++; if (clock.stop) return; } } }
I cant figure it out. Some help?
- 07-24-2012, 09:25 PM #2
Similar Threads
-
Stopwatch return wrong values
By deadManN in forum New To JavaReplies: 9Last Post: 12-22-2011, 01:38 PM -
Urgent: Swing application for stopwatch producing different output after a few runs
By princehektor in forum New To JavaReplies: 7Last Post: 09-20-2011, 05:21 PM -
Collisions are working & not working
By Jayayoh in forum Java 2DReplies: 1Last Post: 06-24-2011, 05:21 PM -
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks