/ Published in: Java
It's a piece of code that calculate the sum from a sequence of number that inserted into the program in a certain duration. This is an example of using multiple threads.
Expand |
Embed | Plain Text
import java.util.Date.*; import java.util.ArrayList; { private String str; private int sum; private ArrayList<Integer> numSequence; private int currentSum; public boolean terminates; private long timeRecord; private int partialSum; public runningSum() { //str=new String (s); numSequence = new ArrayList<Integer>(); terminates = false; sum = 0; partialSum = 0; } public void add(int a) { partialSum = partialSum + a; } public int retrieveV() { if (newTimes - timeRecord > 300000) { return sum; }else { return sum + partialSum; } } public void run () { for (;;) { if (terminates) { break; } try { synchronized (this) { sum = sum + partialSum; partialSum = 0; while (numSequence.size() > 60) { int temp = toRemove.intValue(); sum = sum - temp; } } { } } } { try{ runningSum sumA = new runningSum(); for (int i = 0; i < 100; i++) { sumA.add(100); } sumA.terminates = true; { } } }
You need to login to post a comment.
