java - TimerTask alternative -
basically have 2 classes: main , population. i'm trying increase population.total 100 using population.grow() every second. population extends class can't have extend timertask.
this code population:
public class population extends anotherclass{ private int total = 0; void grow(){ this.population = this.population + 100; } } and main class:
public class main{ public static void main(string [] args){ population population = new population(); } } normally i'd make population extend timer perform updates this:
timer timer = new timer(); timer.schedule(grow(), 1000); the problem neither main nor population can extend timer or other class need population declared inside main class. how can go doing this?
you make implement runnable , use scheduledexecutorservice.
scheduledexecutorservice scheduler = executors.newscheduledthreadpool(1); scheduler.scheduleatfixedrate(yourrunnable, 0, 1, timeunit.seconds);
Comments
Post a Comment