哪家公司做网站正规,如何逐步提升网站权重,湖北省和建设厅网站首页,自己怎么创网站Java中有多种用于多线程的技术。 可以通过同步关键字#xff0c;锁或原子变量来并行化Java中的一段代码。 这篇文章将比较使用synced关键字ReentrantLock#xff0c;getAndIncrement#xff08;#xff09;以及执行get#xff08;#xff09;和compareAndSet#xff08;… Java中有多种用于多线程的技术。 可以通过同步关键字锁或原子变量来并行化Java中的一段代码。 这篇文章将比较使用synced关键字ReentrantLockgetAndIncrement以及执行get和compareAndSet调用的连续试验的性能。 创建了不同类型的Matrix类以进行性能测试其中还包括一个普通类。 为了进行比较在具有Intel Core I7具有8个核心其中4个是真实的Ubuntu 14.04 LTS和Java的计算机上对于不同大小的矩阵具有不同类型的同步线程数和池大小所有单元都增加了100倍。 1.7.0_60。 这是性能测试的简单矩阵类 /**
* Plain matrix without synchronization.
*/
public class Matrix {
private int rows;
private int cols;
private int[][] array;
/**
* Matrix constructor.
*
* param rows number of rows
* param cols number of columns
*/
public Matrix(int rows, int cols) {
this.rows rows;
this.cols cols;
array new int[rows][rows];
}
/**
* Increments all matrix cells.
*/
public void increment() {
for (int i 0; i rows; i) {
for (int j 0; j cols; j) {
array[i][j];
}
}
}
/**
* Returns a string representation of the object which shows row sums of each row.
*
* return a string representation of the object.
*/
Override
public String toString() {
StringBuffer s new StringBuffer();
int rowSum;
for (int i 0; i rows; i) {
rowSum 0;
for (int j 0; j cols; j) {
rowSum array[i][j];
}
s.append(rowSum);
s.append( );
}
return s.toString();
}
} 对于其他矩阵由于每种矩阵类型的剩余部分相同因此列出了它们的增量方法。 同步矩阵 public void increment() {
for (int i 0; i rows; i) {
for (int j 0; j cols; j) {
synchronized (this) {
array[i][j];
}
}
}
} 锁矩阵 public void increment() {
for (int i 0; i rows; i) {
for (int j 0; j cols; j) {
lock.lock();
try {
array[i][j];
} finally {
lock.unlock();
}
}
}
} 原子getAndIncrement矩阵 public void increment() {
for (int i 0; i rows; i) {
for (int j 0; j cols; j) {
array[i][j].getAndIncrement();
}
}
} 连续尝试get和compareAndSet矩阵 public void increment() {
for (int i 0; i rows; i) {
for (int j 0; j cols; j) {
for (; ; ) {
int current array[i][j].get();
int next current 1;
if (array[i][j].compareAndSet(current, next)) {
break;
}
}
}
}
} 还为每个矩阵创建了工人类别。 这是普通工人阶级 /**
* Worker for plain matrix without synchronization.
*
* author Furkan KAMACI
* see Matrix
*/
public class PlainMatrixWorker extends Matrix implements Runnable {
private AtomicInteger incrementCount new AtomicInteger(WorkerDefaults.INCREMENT_COUNT);
/**
* Worker constructor.
*
* param rows number of rows
* param cols number of columns
*/
public PlainMatrixWorker(int rows, int cols) {
super(rows, cols);
}
/**
* Increments matrix up to a maximum number.
*
* see WorkerDefaults
*/
Override
public void run() {
while (incrementCount.getAndDecrement() 0) {
increment();
}
}
} 为了进行正确的比较默认情况下所有测试都会被重复20次。 计算每个结果的平均和标准误差。 由于测试集有很多维度矩阵类型矩阵大小池大小线程数和经过时间因此某些功能在图表中汇总显示。 结果如下对于池大小2和线程数2 对于池大小4和线程数4 对于池大小6和线程数6 对于池大小8和线程数8 对于池大小10和线程数10 对于池大小12和线程数12 结论 可以很容易地看到普通版本运行最快。 但是它不会产生预期的正确结果。 同步块的性能更差使用“ this ”完成同步时。 锁比同步块稍好。 但是原子变量在所有变量中都明显更好。 当原子getAndIncrement以及对get和compareAndSet调用的连续试验进行比较时表明它们的性能相同。 检查Java源代码时很容易理解其背后的原因 /**
* Atomically increments by one the current value.
*
* return the previous value
*/
public final int getAndIncrement() {
for (;;) {
int current get();
int next current 1;
if (compareAndSet(current, next))
return current;
}
} 可以看出在Java1.7版源代码中通过对get和compareAndSet进行连续试验来实现getAndIncrement。 另一方面当检查其他结果时可以看到池大小的影响。 当使用的池大小小于实际线程数时将发生性能问题。 因此Java中多线程的性能比较表明当确定要同步一段代码并且出现性能问题时如果像测试中那样使用此类线程则应尝试使用Atomic变量。 其他选择应该是锁或同步块。 同样这并不意味着由于JIT编译器的影响并且多次运行一段代码同步块总是比锁更好。 可以从此处下载用于Java多线程性能比较的源代码 https : //github.com/kamaci/performance 翻译自: https://www.javacodegeeks.com/2015/05/performance-comparison-of-multithreading-in-java.html