不做任何安全措施下的代码
public class IncrementTest {
private static int inc = 0;
private static void increase() {
inc++;
}
public static void main(String[] params) throws Exception {
for (int num = 0; num < 10; num++) {
Thread firstThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread secThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread thirdThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
firstThread.start();
secThread.start();
thirdThread.start();
firstThread.join();
secThread.join();
thirdThread.join();
System.out.print(inc + " ");
inc = 0;
}
} } //result:280957 300000 300000 265487 300000 300000 300000 300000 300000 300000
弱同步 volatile
public class IncrementTest {
private static volatile int inc = 0;
private static void increase() {
inc++;
}
public static void main(String[] params) throws Exception {
for (int num = 0; num < 10; num++) {
Thread firstThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread secThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread thirdThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
firstThread.start();
secThread.start();
thirdThread.start();
firstThread.join();
secThread.join();
thirdThread.join();
System.out.print(inc + " ");
inc = 0;
}
}} //result:271160 300000 300000 219991 171981 300000 300000 300000 300000 265862
互斥同步(悲观锁)synchronized
public class IncrementTest {
private static int inc = 0;
private synchronized static void increase() {
inc++;
}
public static void main(String[] params) throws Exception {
for (int num = 0; num < 10; num++) {
Thread firstThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread secThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread thirdThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
firstThread.start();
secThread.start();
thirdThread.start();
firstThread.join();
secThread.join();
thirdThread.join();
System.out.print(inc + " ");
inc = 0;
}
}} //result:300000 300000 300000 300000 300000 300000 300000 300000 300000 300000
非阻塞同步(乐观锁)
public class IncrementTest {
private static AtomicInteger inc = new AtomicInteger(0);
private synchronized static void increase() {
inc.getAndIncrement();
}
public static void main(String[] params) throws Exception {
for (int num = 0; num < 10; num++) {
Thread firstThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread secThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
Thread thirdThread = new Thread(() -> {
for (int j = 0; j < 100000; j++) {
increase();
}
});
firstThread.start();
secThread.start();
thirdThread.start();
firstThread.join();
secThread.join();
thirdThread.join();
System.out.print(inc + " ");
inc.set(0);
}
}} //result:300000 300000 300000 300000 300000 300000 300000 300000 300000 300000
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。