本文是异步编程学习之路(二)-通过Synchronized实现线程安全的多线程,若要关注前文,请点击传送门:
异步编程学习之路(一)-通过Thread实现简单多线程(线程不安全)
前文我们通过Thread实现了几种线程不安全的多线程写法,导致最后的结果与预期的值不一样。
一、通过Synchronized实现安全的多线程
/**
* @Description:简单多线程的实现(线程安全)
* @Author:zhangzhixiang
* @CreateDate:2018/12/21 20:30:54
* @Version:1.0
*/
public class TestThread {
private int count = 0;
public static void main(String[] args) {
TestThread testThread = new TestThread();
testThread.calculation();
}
public void calculation() {
List<Thr