Java线程同步操作
2018-03-03 00:00
417 查看
摘要: Java线程同步操作
作用于实例方法:相当于对当前实例加锁,进入同步代码前要获得当前实例的锁。
作用于静态方法:相当于对当前类加锁,进入同步代码前要获得当前类的锁。
假设把给类实例加锁中的每个实例比作一个门,上面的测试方法中每个门都有锁但是10个门10把锁,每个线程进一个门。还是不能保证临界区资源i同时只一个线程访问
fix
上面测试的testInteger方法和testIntegerFix方法都能得到正确的结果,原因是给静态类方法加锁相当于10个门用的同一把锁,保证了同一时间只有一个线程能访问临界区资源i。
synchronized
作用于对象实例:对给定对象加锁,进入同步代码前要获得给定对象的锁。作用于实例方法:相当于对当前实例加锁,进入同步代码前要获得当前实例的锁。
作用于静态方法:相当于对当前类加锁,进入同步代码前要获得当前类的锁。
使用
给实例对象加锁
public class AccountingSync implements Runnable { static AccountingSync instance = new AccountingSync(); static int i = 0; @Override public void run() { for (int k = 0; k < 10000; k++) { synchronized (instance) { i++; } } } @Test public void testInteger() throws InterruptedException { int count = 10; Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(instance); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); } }
给类方法加锁
public class AccountingSync2 implements Runnable { static AccountingSync2 instance = new AccountingSync2(); static int i = 0; public synchronized void increase() { i++; } @Override public void run() { for (int k = 0; k < 10000; k++) { increase(); } } @Test public void testInteger() throws InterruptedException { int count = 10; Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(instance); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); } }
给类方法加锁的错误演示
public class AccountingSyncBad implements Runnable { static int i = 0; public synchronized void increase() { i++; } @Override public void run() { for (int k = 0; k < 10000; k++) { increase(); } } @Test public void testInteger() throws InterruptedException { int count = 10; Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(new AccountingSyncBad()); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); } }
假设把给类实例加锁中的每个实例比作一个门,上面的测试方法中每个门都有锁但是10个门10把锁,每个线程进一个门。还是不能保证临界区资源i同时只一个线程访问
fix
@Test public void testIntegerFix() throws InterruptedException { int count = 10; AccountingSyncBad instance = new AccountingSyncBad(); Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(instance); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); }
给静态类方法加锁
public class AccountingSyncClass implements Runnable { static int i = 0; public static synchronized void increase() { i++; } @Override public void run() { for (int k = 0; k < 10000; k++) { increase(); } } @Test public void testInteger() throws InterruptedException { int count = 10; Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(new AccountingSyncClass()); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); } @Test 7fe0 public void testIntegerFix() throws InterruptedException { int count = 10; AccountingSyncClass instance = new AccountingSyncClass(); Thread[] ts = new Thread[count]; for (int k = 0; k < count; k++) { ts[k] = new Thread(instance); } // start for (int k = 0; k < count; k++) { ts[k].start(); } // join for (int k = 0; k < count; k++) { ts[k].join(); } System.out.println(i); } }
上面测试的testInteger方法和testIntegerFix方法都能得到正确的结果,原因是给静态类方法加锁相当于10个门用的同一把锁,保证了同一时间只有一个线程能访问临界区资源i。
相关文章推荐
- JAVA多线程—CountDownLatch-一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
- Java线程:线程的同步与锁 一、同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏。 例如:两个线程ThreadA、ThreadB都操作同一个对象Foo对
- java线程同步中使用自增操作时的注意
- java线程同步操作实例详解
- java,线程操作,同步访问共享的可变数据
- JAVA线程同步锁和线程间协调操作
- Java 并发(一)——线程和同步
- Java多线程-线程的同步(同步方法)
- 从控制权开始讨论java线程的同步方案。
- 用Java线程获取优异性能(II)——使用同步连载线程访问关键代码部份
- java线程同步
- Java并发原语——线程、互斥与同步
- java线程同步
- Java5对线程处理的新操作-concurrent包介绍
- java线程同步
- java 线程的同步
- Java线程同步synchronized和volatile
- java线程同步与通信
- Java线程:线程的同步-同步块
- java线程同步