java - How can I access the property value of a running Thread -
i have thread run long time (5-6 min).
public class exportthread implements runnable { private integer totalexport = 0; @override public void run() { try { exportcorner(); } catch (exception e) { e.printstacktrace(); } } private void exportcorner() { list<myobject> list = ...... for(myobject o : list){ ....... totalexport++; } } }
this thread runs class. how can totalexport again , again after small interval ?
simply adding getters thread class, @romankonoval said need synchronized
public class exportthread implements runnable { private integer totalexport = 0; @override public void run() { try { exportcorner(); } catch (exception e) { e.printstacktrace(); } } private void exportcorner() { list<integer> list = ...... for(integer : list){ totalexport += i; } } public synchronized integer gettotalexport(){ return this.totalexport; } }
from other class can use that
import java.util.*; public class mainclass { public static void main(string[] args) { exportthread exportthread = new exportthread(); thread t = new thread(exportthread); t.start(); timertask timertask= new mytimertask(exportthread); timer timer = new timer(); timer.scheduleatfixedrate(tasknew,new date(),1000); } }
then may need implement timer task
public class mytimertask extends timertask { private exportthread exportthread; public mytimertask(exportthread exportthread){ this.exportthread = exportthread; } @override public void run() { system.out.println(exportthread.gettotalexports()); } }
Comments
Post a Comment