本文整理匯總了Java中java.util.concurrent.atomic.AtomicInteger.getAndAdd方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicInteger.getAndAdd方法的具體用法?Java AtomicInteger.getAndAdd怎麽用?Java AtomicInteger.getAndAdd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.atomic.AtomicInteger
的用法示例。
在下文中一共展示了AtomicInteger.getAndAdd方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: CacheManager
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private CacheManager(final File cacheDir, final long sizeLimit, final int countLimit) {
this.cacheDir = cacheDir;
this.sizeLimit = sizeLimit;
this.countLimit = countLimit;
cacheSize = new AtomicLong();
cacheCount = new AtomicInteger();
mThread = new Thread(new Runnable() {
@Override
public void run() {
int size = 0;
int count = 0;
final File[] cachedFiles = cacheDir.listFiles();
if (cachedFiles != null) {
for (File cachedFile : cachedFiles) {
size += cachedFile.length();
count += 1;
lastUsageDates.put(cachedFile, cachedFile.lastModified());
}
cacheSize.getAndAdd(size);
cacheCount.getAndAdd(count);
}
}
});
mThread.start();
}
示例2: sliceInvoked
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
@Override
public MetricData sliceInvoked() {
if (elapsed_distribution.length == 0) {
return null;
}
JSFElapsedData data = new JSFElapsedData(interfaceId, methodName, providerIp, consumerIp);
data.setElapsedMetricsId(elaspedMetricsId);
int[] distribution = new int[elapsed_distribution.length];
for (int i = 0; i < elapsed_distribution.length; i++) {
AtomicInteger counter = elapsed_distribution[i];
int count = counter.get();
distribution[i] = count;
if (count > 0) {
counter.getAndAdd(-count); // 減去已經統計的值
}
}
data.setDistribution(distribution);
return data;
}
示例3: test
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
@Test
public void test() throws InterruptedException {
final AtomicInteger atomic = new AtomicInteger();
Runnable task = new Runnable() {
@Override
public void run() {
int i = atomic.getAndAdd(1);
String encrypt = EncryptUtil.encrypt("" + i);
System.out.println(EncryptUtil.decrypt(encrypt));
}
};
ExecutorService executor = Executors.newFixedThreadPool(20);
for (int i = 0; i < 20; i++) {
executor.execute(task);
}
Thread.sleep(3000);
}
示例4: verifyValuesOnAllVMs
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
protected void verifyValuesOnAllVMs(String tableName) {
AtomicInteger actualRows = new AtomicInteger(0);
final ArrayList<VM> vmList = new ArrayList<>(Arrays.asList(vm0, vm1, vm2));
for (int i = 0; i < vmList.size(); i++) {
final int res = (int) vmList.get(i).invoke(new SerializableCallable() {
@Override
public Object call() throws Exception {
return verifyValues(tableName);
}
});
actualRows.getAndAdd(res);
}
assertEquals(NUM_ROWS, actualRows.get());
}
示例5: roundRobinSelect
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
public static HostInfo roundRobinSelect(String serviceName)
{
ArrayList<HostInfo> hostInfos=LOCALSERVER_HOSTINFOS.get(serviceName);
if(hostInfos!=null&&hostInfos.size()>0)
{
AtomicInteger atomicIndex=ROUNDROBIN_INDEX.get(serviceName);
if(atomicIndex.get()==hostInfos.size())
{
atomicIndex.set(0);
}
int index=atomicIndex.getAndAdd(1);
return hostInfos.get(index);
}
return null;
}
示例6: startLocalTransform
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private void startLocalTransform(){
//Initialize an array to store the local Hough transform from each thread
localHoughValues = new int [nCirlcesPrev][2*searchRadius + 1][2*searchRadius + 1][2*searchBand + 1];
//Initialize an array to store the final dimensions and location of each local Hough Transform
localHoughParameters = new int [nCirlcesPrev][9];
//Setup two processing pipelines to multithread only if there are more than 1 circles
if(nCirlcesPrev < 2){
localHoughTransform(0);
localGetCenterPoint(0); //Make sure this returns -1 to index if no circle was found
maxHough = houghScores[0];
}
//Otherwise, multithread the local search
else{
//Build an array to store the result from each thread
final Thread[] threads = newThreadArray();
//Create an atomic integer counter that each thread can use to count through the radii
final AtomicInteger ai = new AtomicInteger(0);
//Build a thread for as many CPUs as are available to the JVM
for (ithread = 0; ithread < threads.length; ithread++) {
// Concurrently run in as many threads as CPUs
threads[ithread] = new Thread() {
{ setPriority(Thread.NORM_PRIORITY); }
@Override
public void run() {
//Divide the task so that each core works on a subset of circles
for(int circleNum = ai.getAndAdd(1); circleNum < nCirlcesPrev; circleNum = ai.getAndAdd(1)){
//Check for interrupt
if(cancelThread) return;
localHoughTransform(circleNum);
localGetCenterPoint(circleNum); //Make sure this returns -1 to index if no circle was found
}
}
};
}
startAndJoin(threads);
//Retrieve the maximum hough value if the raw Hough is desired
if(houghSeries){
for(int i=0; i<nCirlcesPrev; i++){
if(houghScores[i] > maxHough) maxHough = houghScores[i];
}
}
}
//Flag move all circles to the starting indexes, so there are no gaps between circles (i.e. if there were 3 circles, then they occupy indeces 0-2)
collapseLocalResult();
}
示例7: startPartialLocalSearch
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private void startPartialLocalSearch(){
//Build an array to store the result from each thread
final Thread[] threads = newThreadArray();
//Create an atomic integer counter that each thread can use to count through the radii
final AtomicInteger ai = new AtomicInteger(0);
//Build a thread for as many CPUs as are available to the JVM
for (ithread = 0; ithread < threads.length; ithread++) {
// Concurrently run in as many threads as CPUs
threads[ithread] = new Thread() {
{ setPriority(Thread.NORM_PRIORITY); }
@Override
public void run() {
//Divide the task so that each core works on a subset of circles
for(int circleNum = ai.getAndAdd(1); circleNum < nCirlcesPrev; circleNum = ai.getAndAdd(1)){
getLocalCenterPoint2(circleNum);
//Check for interrupt
if(cancelThread) return;
}
}
};
}
startAndJoin(threads);
//Clear out the remainder of the previous circles in the circle information arrays
for(int a = nCirlcesPrev; a<circleID.length; a++){
circleID[a] = -1;
centerPoint[a] = new Point(-1,-1);
centerRadii[a] = -1;
houghScores[a] = -1;
}
//Flag move all circles to the starting indexes, so there are no gaps between circles (i.e. if there were 3 circles, then they occupy indeces 0-2)
collapseLocalResult();
}
示例8: houghTransform
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private void houghTransform () {
//Update progress bar string with current task
if(isGUI) publish("Performing full Hough transform...");
IJ.showStatus("Performing full Hough transform...");
//Build an array to store the result from each thread
final Thread[] threads = newThreadArray();
//Create an atomic integer counter that each thread can use to count through the radii
final AtomicInteger ai = new AtomicInteger(radiusMin);
final AtomicInteger progress = new AtomicInteger(0);
final AtomicInteger lastProgress = new AtomicInteger(0);
//Create an array to store the Hough values
houghValues = new int[width][height][depth];
//Create a variable for storing the total progress possible (100 = complete)
//Nute depth is devided by nCPUs, therefore depth*nCPUs/nCPUs = depth
double totalProgress = height*depth/100;
//Build a thread for as many CPUs as are available to the JVM
for (ithread = 0; ithread < threads.length; ithread++) {
// Concurrently run in as many threads as CPUs
threads[ithread] = new Thread() {
{ setPriority(Thread.NORM_PRIORITY); }
@Override
public void run() {
//Divide the radius tasks across the cores available
int currentProgress = 0;
for (int radius = ai.getAndAdd(radiusInc); radius <= radiusMax; radius = ai.getAndAdd(radiusInc)) {
int indexR=(radius-radiusMin)/radiusInc;
//For a given radius, transform each pixel in a circle, and add-up the votes
for(int y = 1; y < height-1; y++) {
//Increment the progress counter, and submit the current progress status
progress.getAndAdd(1);
//Calculate the current progress value
currentProgress = Math.round((float) (progress.get()/totalProgress));
//There is a significant time penalty for progress updates, so only update if needed
if(currentProgress > lastProgress.get()){ //7.8s with if, 8.7s without if, 7.8s with no progress update, 8.7s with delay between GUI updates
if(isGUI && currentProgress <= 100) setProgress(currentProgress);
IJ.showProgress(currentProgress, 100);
lastProgress.set(currentProgress);
}
//Check for interrupt
if(cancelThread) return;
for(int x = 1; x < width-1; x++) {
if( imageValues[(x+offx)+(y+offy)*fullWidth] != 0 ) {// Edge pixel found
for(int i = 0; i < lutSize; i++) {
int a = x + lut[1][i][indexR];
int b = y + lut[0][i][indexR];
if((b >= 0) & (b < height) & (a >= 0) & (a < width)) {
houghValues[a][b][indexR] += 1;
}
}
}
}
}
}
}
};
}
startAndJoin(threads);
}
示例9: houghMaximum
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private void houghMaximum () {
//long startTime = System.currentTimeMillis(); //1337ms without multi, 319ms with multi, 175ms by writing to private variable per thread
//Build an array to store the result from each thread
final Thread[] threads = newThreadArray();
//Build an array to store the max from each thread
maxHoughArray = new int[threads.length][4];
//Create an atomic integer counter that each thread can use to count through the radii
final AtomicInteger ai = new AtomicInteger(0);
final AtomicInteger progress = new AtomicInteger(0);
final AtomicInteger lastProgress = new AtomicInteger(0);
//Create an integer for indexing the results array (one result per thread
final AtomicInteger Index = new AtomicInteger(0);
//Create a variable for storing the total progress possible (100 = complete)
//Nute depth is devided by nCPUs, therefore depth*nCPUs/nCPUs = depth
double totalProgress = height*depth/100;
maxHough = 0;
//Build a thread for as many CPUs as are available to the JVM
for (ithread = 0; ithread < threads.length; ithread++) {
// Concurrently run in as many threads as CPUs
threads[ithread] = new Thread() {
{ setPriority(Thread.NORM_PRIORITY); }
//Search for the largest score with each thread
@Override
public void run() {
int maxHoughThread = -1;
int maxRadiusThread = -1;
int currentProgress = 0;
Point maxPointThread = new Point (-1,-1);
for(int a=ai.getAndIncrement(); a<depth; a=ai.getAndIncrement()){
for(int j = 0; j < height; j++) {
//Increment the progress counter, and submit the current progress status
progress.getAndAdd(1);
//Gui updates can be time intensive, so only update at fixed time intervals
currentProgress = Math.round((float) (progress.get()/totalProgress));
//There is a significant time penalty for progress updates, so only update if needed
if(currentProgress > lastProgress.get() & currentProgress >= 0 & currentProgress <= 100){ //7.8s with if, 8.7s without if, 7.8s with no progress update, 8.7s with delay between GUI updates
if(isGUI) setProgress(currentProgress);
IJ.showProgress(currentProgress, 100);
lastProgress.set(currentProgress);
}
//Check for interrupt
if(cancelThread) return;
for(int k = 0; k < width; k++){
if(houghValues[k][j][a] > maxHoughThread) {
maxHoughThread = houghValues[k][j][a];
maxPointThread = new Point(k,j);
maxRadiusThread = a*radiusInc + radiusMin;
}
}
}
}
//Have each thread report the score to a common array
maxHoughArray[Index.getAndIncrement()] = new int[]{maxHoughThread, maxRadiusThread, maxPointThread.x, maxPointThread.y};
}
};
}
startAndJoin(threads);
//Search common array for highest score
for (int[] maxHoughArray1 : maxHoughArray) {
if (maxHoughArray1[0] > maxHough) {
maxHough = maxHoughArray1[0];
maxRadius = maxHoughArray1[1];
maxPoint = new Point((int) maxHoughArray1[2], (int) maxHoughArray1[3]);
}
}
}
示例10: HoughSpaceSeries
import java.util.concurrent.atomic.AtomicInteger; //導入方法依賴的package包/類
private void HoughSpaceSeries(int slice, ImageStack houghStack){
//If the maximum Hough value has not yet been assigned, search the whole Hough transform for the maximum value
if(maxHough == -1){
houghMaximum();
}
//Create an array to store the Hough values
byte localHoughPixels[][] = new byte[depth][width*height];
int startFrame = ((slice-1)*depth);
//If a full transform was done, calcualte the Hough Pixels
if(minCircles > 0 | nCirlcesPrev > 0){
//Build an array to store the result from each thread
final Thread[] threads = newThreadArray();
//Create an atomic integer counter that each thread can use to count through the radii
final AtomicInteger ai = new AtomicInteger(radiusMin);
//Build a thread for as many CPUs as are available to the JVM
for (ithread = 0; ithread < threads.length; ithread++) {
// Concurrently run in as many threads as CPUs
threads[ithread] = new Thread() {
{ setPriority(Thread.NORM_PRIORITY); }
@Override
public void run() {
//Divide the radius tasks across the cores available
for (int radius = ai.getAndAdd(radiusInc); radius <= radiusMax; radius = ai.getAndAdd(radiusInc)) {
//Check for interrupt
if(cancelThread) return;
//Calculate the corresponding index
int houghIndex = (radius-radiusMin)/radiusInc;
//If full tansform was performed, retrieve the pixel array for the current Hough radius image
createHoughPixels(localHoughPixels[houghIndex], houghIndex);
//Deposit the array image into the corresponding slice in the stack
houghStack.setPixels(localHoughPixels[houghIndex], houghIndex+1+startFrame);
//Give the current slice the appropriate radius label
houghStack.setSliceLabel("Hough Space [r="+radius+", resolution="+resolution+"]", houghIndex+1+startFrame);
}
}
};
}
startAndJoin(threads);
}
//Deposit the array into the Hough Series stack
//Not time limiting, even at fairly high resolutions
for(int radius = radiusMin; radius<=radiusMax; radius += radiusInc) {
//Check for interrupt
if(cancelThread) return;
//Calculate the corresponding index
int houghIndex = (radius-radiusMin)/radiusInc;
//Deposit the array image into the corresponding slice in the stack
houghStack.setPixels(localHoughPixels[houghIndex], houghIndex+1+startFrame);
//Give the current slice the appropriate radius label
houghStack.setSliceLabel("Hough Space [r="+radius+", resolution="+resolution+"]", houghIndex+1+startFrame);
}
}