java - How to get time in following fragment code -



java - How to get time in following fragment code -

why gettime() method homecoming null value? returning non-null value when time declared static variable, can not create objects different time.

package timecounter; import java.util.timer; import java.util.timertask; public class timerx extends timertask { int h, m, s; timer timer; string time; @override public void run() { s++; if (s >= 60) { m++; s = 0; if (m >= 60) { h++; m = 0; if (h >= 12) { h = 0; } } } this.time = string.format("%2d:%2d:%2d", h, m, s); system.out.print("running"); } public void start() { timer.schedule(new timerx(), 1000, 1000); } public timerx() { this.timer = new timer(); } public string gettime() {// here homecoming null value homecoming time; } }

replace:

timer.schedule(new timerx(), 1000, 1000);

with:

timer.schedule(this, 1000, 1000);

however peace of code not nice. want have multiple timer counters each timertask has own timer. each timer needs thread services. improve utilize multiple timertasks 1 timer.

e.g. replace

timer timer;

with:

static timer timer = new timer();

or extract separate singleton class e.g. counterservice.

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -