Reputation: 867
I have a Thread that run for a 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 from another class. How can I get the totalExport again and again after a small interval ?
Upvotes: 1
Views: 562
Reputation: 2048
Simply you get it by adding some getters to your thread class, as @RomanKonoval said it need to be 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 i : list){
totalExport += i;
}
}
public synchronized Integer getTotalExport(){
return this.totalExport;
}
}
From your other class you can use something like 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 you may need to implement your 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());
}
}
Upvotes: 3
Reputation: 38910
A lock free solution using AtomicInteger
import java.util.*;
import java.util.concurrent.atomic.*;
public class ExportThread implements Runnable {
private AtomicInteger totalExport = new AtomicInteger(0);
@Override
public void run() {
try {
exportCorner();
} catch (Exception e) {
e.printStackTrace();
}
}
private void exportCorner() {
List<MyObject> list = new ArrayList<MyObject>(10);
list.add(new MyObject());
list.add(new MyObject());
for(MyObject o : list){
totalExport.getAndIncrement();
}
System.out.println("total export:"+getTotalExport());
}
public Integer getTotalExport(){
return totalExport.get();
}
public static void main(String args[]){
ExportThread t = new ExportThread();
new Thread(t).start();
}
}
class MyObject {
}
output:
total export:2
Steps:
AtomicInteger
Related SE question:
Practical uses for AtomicInteger
Upvotes: 2
Reputation: 4558
If I understand your problem correctly, it really looks like Observer design pattern :
Documentation : https://en.wikipedia.org/wiki/Observer_pattern
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern.[1] The observer pattern is implemented in numerous programming libraries and systems, including almost all GUI toolkits.
Upvotes: -1