同步的前提:

  1. 必须要有两个或者两个以上的线程
  2. 必须是多个线程使用同一个锁
  3. 必须保证同步中只能有一个线程在运行
  • 好处:解决了多线程的安全问题
  • 弊端:多个线程需要判断锁,较为消耗资源、抢锁的资源。
import java.util.ArrayList;
import java.util.List; /**
* synchronized 实现线程间的同步,对同步的代码加锁,似的每次只能有一个线程进入同步块
* 可以保证线程间的可见性和有序性
* · 指定加锁对象:对给定对象加锁,进入同步代码前要获取给定对象的锁
* · 直接作用于实例方法:相当于对当前实例加锁,进入同步代码前要获取当前实例的锁
* · 直接作用于静态方法:相当于对当前类加锁,进入同步代码前要获取当前类的锁 -- class文件
*/
public class SynchronizedDemo {
private static int size = 0;
private static List<Integer> arrayOne = new ArrayList<>(10000);
private static List<Integer> arrayTwo = new ArrayList<>(10000);
public static synchronized void increase(){ //synchronized作用于一个实例方法
size++;
}
public static void main(String[] args) throws InterruptedException{ for (int i = 0; i < 10000; i++) {
arrayOne.add(0);
arrayTwo.add(0);
}
Thread threadOne = new Thread(() -> {
int length = arrayOne.size();
for (int i = 0; i < length; i++) {
if (arrayOne.get(i).intValue()==0){
increase();
}
}
});
Thread threadTwo = new Thread(() -> {
int length = arrayTwo.size();
for (int i = 0; i < length; i++) {
if (arrayTwo.get(i).intValue()==0){
increase();
}
}
});
threadOne.start();
threadTwo.start();
threadOne.join();
threadTwo.join();
System.out.println(size);
}
}
import java.util.ArrayList;
import java.util.List; /**
* Runnable
*/
public class SynchronizedDemo1 {
private static int size = 0;
private static List<Integer> arrayOne = new ArrayList<>(10000);
private static List<Integer> arrayTwo = new ArrayList<>(10000); public static class AccountSyncBad implements Runnable{
synchronized void increase(){ //synchronized
size++;
}
@Override
public void run() {
int length = arrayOne.size();
for (int i = 0; i < length; i++) {
if (arrayOne.get(i).intValue()==0){
increase();
}
}
}
}
public static void main(String[] args) throws InterruptedException{
for (int i = 0; i < 10000; i++) {
arrayOne.add(0);
arrayTwo.add(0);
}
AccountSyncBad accountSyncBad = new AccountSyncBad();
Thread threadOne = new Thread(accountSyncBad);
Thread threadTwo = new Thread(accountSyncBad);
threadOne.start();
threadTwo.start();
threadOne.join();
threadTwo.join();
System.out.println(size);
}
}
import java.util.ArrayList;
import java.util.List; /**
* 两个线程指向不同的Runnable实例,这两个线程使用的是两把不同的锁,无法保证线程安全
*/
public class SynchronizedDemo2 {
private static int size = 0;
private static List<Integer> arrayOne = new ArrayList<>(10000);
private static List<Integer> arrayTwo = new ArrayList<>(10000); public static class AccountSyncBad implements Runnable{
synchronized void increase(){ //synchronized
size++;
}
// static synchronized void increase(){ //修改为static,这样即使两个线程指向不同的Runnable,但请求的是当前类的锁,因此可以正确同步
// size++;
// }
@Override
public void run() {
int length = arrayOne.size();
for (int i = 0; i < length; i++) {
if (arrayOne.get(i).intValue()==0){
increase();
}
}
}
}
public static void main(String[] args) throws InterruptedException{
for (int i = 0; i < 10000; i++) {
arrayOne.add(0);
arrayTwo.add(0);
}
Thread threadOne = new Thread(new AccountSyncBad());
Thread threadTwo = new Thread(new AccountSyncBad());
threadOne.start();
threadTwo.start();
threadOne.join();
threadTwo.join();
System.out.println(size);
}
}

最新文章

  1. Win10搭建Linux开发环境之网络连接设定
  2. QWebView在 Qt 5.x中编译出错:File not found: main.obj
  3. mvvm架构使用解析
  4. C#应用程序获取项目路径的方法总结
  5. Gold Balanced Lineup(哈希表)
  6. 查看Linux相关信息
  7. Ubuntu12.04 Jdk1.7 Tomct7.0部署配置
  8. JS 某一区域内所有CheckBox全选和取消全选(.net)
  9. Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边
  10. 安装php扩展后,执行时找不到扩展 class xxx no found
  11. 建造者(Builder)模式
  12. 微信公众平台开发,图文回复、access_token生成调用、以及微信SDK的实现(2)
  13. 【Git】Git使用记录: remove *.lock eg: index.lock/head.lock
  14. 腾讯广告联盟 Android SDK(广点通)demo的使用方式
  15. SQL Server 权限控制
  16. MVC _Ajax的使用【七】
  17. Set ARITHABORT Option设置为ON
  18. 报错:Column count doesn&#39;t match value count at row 1
  19. Python黑魔法
  20. C#写的COM组件注册问题兼论微软Regasm注册的BUG

热门文章

  1. Hibernate入门核心配置文件和orm元数据配置文件详解
  2. shiro 安全框架 详解
  3. Angular JS - 7 - Angular JS 常用指令2
  4. HDU - 6621 K-th Closest Distance 主席树+二分答案
  5. Radical and array
  6. [CSP-S模拟测试]:密码(AC自动机+DP)
  7. mysql创建,添加主键
  8. fatal: early EOF fatal: index-pack failed &amp; Git, fatal: The remote end hung up unexpectedly
  9. appium常见问题10_MAC_终端输入aapt指令报错提示&quot;command not found&quot;
  10. appium常见问题08_pycharm中导入appium报错(&amp;#160;已成功安装appium_python_client)【MAC】