一、synchronized关键字
1、锁重入
1.1、调用其他同步方法
同步方法 - 调用其他同步方法 锁可重入。 同一个线程,多次调用同步代码,锁定同一个锁对象,可重入。
/**
* synchronized关键字
* 同步方法 - 调用其他同步方法
* 锁可重入。 同一个线程,多次调用同步代码,锁定同一个锁对象,可重入。
*/
package concurrent.t01;
import java.util.concurrent.TimeUnit;
public class Test_06 {
synchronized void m1(){ // 锁this
System.out.println("m1 start");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
m2();
System.out.println("m1 end");
}
synchronized void m2(){ // 锁this
System.out.println("m2 start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m2 end");
}
public static void main(String[] args) {
new Test_06().m1();
}
}
复制代码
结果:
m1 start
m2 start
m2 end
m1 end
复制代码
1.2、继承
/**
* synchronized关键字
* 同步方法 - 继承
* 子类同步方法覆盖父类同步方法。可以指定调用父类的同步方法。
* 相当于锁的重入。
*/
package concurrent.t01;
import java.util.concurrent.TimeUnit;
public class Test_07 {
synchronized void m(){
System.out.println("Super Class m start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Super Class m end");
}
public static void main(String[] args) {
new Sub_Test_07().m();
}
}
class Sub_Test_07 extends Test_07{
synchronized void m(){
System.out.println("Sub Class m start");
super.m();
System.out.println("Sub Class m end");
}
}
复制代码
结果:
Sub Class m start
Super Class m start
Super Class m end
Sub Class m end
复制代码
2、锁与异常
当同步方法中发生异常的时候,自动释放锁资源。不会影响其他线程的执行。
/**
* synchronized关键字
* 同步方法 - 锁与异常
* 当同步方法中发生异常的时候,自动释放锁资源。不会影响其他线程的执行。
* 注意,同步业务逻辑中,如果发生异常如何处理。
*/
package concurrent.t01;
import java.util.concurrent.TimeUnit;
public class Test_08 {
int i = 0;
synchronized void m(){
System.out.println(Thread.currentThread().getName() + " - start");
while(true){
i++;
System.out.println(Thread.currentThread().getName() + " - " + i);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i == 5){
i = 1/0;
}
}
}
public static void main(String[] args) {
final Test_08 t = new Test_08();
new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "t1").start();
new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "t2").start();
}
}
复制代码
结果:
t1 - start
t1 - 1
t1 - 2
t1 - 3
t1 - 4
t1 - 5
t2 - start
t2 - 6
Exception in thread "t1" java.lang.ArithmeticException: / by zero
at concurrent.t01.Test_08.m(Test_08.java:25)
at concurrent.t01.Test_08$1.run(Test_08.java:35)
at java.lang.Thread.run(Unknown Source)
t2 - 7
t2 - 8
t2 - 9
t2 - 10
t2 - 11
t2 - 12
复制代码
3、锁对象变更问题
/**
* synchronized关键字
* 锁对象变更问题
* 同步代码一旦加锁后,那么会有一个临时的锁引用执行锁对象,和真实的引用无直接关联。
* 在锁未释放之前,修改锁对象引用,不会影响同步代码的执行。
*/
package concurrent.t01;
import java.util.concurrent.TimeUnit;
public class Test_13 {
Object o = new Object();
int i = 0;
int a(){
try{
/*
* return i ->
* int _returnValue = i; // 0;
* return _returnValue;
*/
return i;
}finally{
i = 10;
}
}
void m(){
System.out.println(Thread.currentThread().getName() + " start");
synchronized (o) {
while(true){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " - " + o);
}
}
}
public static void main(String[] args) {
final Test_13 t = new Test_13();
new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "thread1").start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}, "thread2");
t.o = new Object();
thread2.start();
System.out.println(t.i);
System.out.println(t.a());
System.out.println(t.i);
}
}
复制代码
结果:锁对象和方法中的锁不是同一个
thread1 start
thread1 - java.lang.Object@5897451a
thread1 - java.lang.Object@5897451a
thread1 - java.lang.Object@5650b078
0
0
10
thread2 start
thread2 - java.lang.Object@5650b078
thread1 - java.lang.Object@5650b078
thread2 - java.lang.Object@5650b078
thread1 - java.lang.Object@5650b078
thread2 - java.lang.Object@5650b078
thread1 - java.lang.Object@5650b078
复制代码
3、常量问题
/**
* synchronized关键字
* 常量问题
* 在定义同步代码块时,不要使用常量对象作为锁对象。
*/
package concurrent.t01;
public class Test_14 {
String s1 = "hello";
String s2 = new String("hello"); // new关键字,一定是在堆中创建一个新的对象。
Integer i1 = 1;
Integer i2 = 1;
void m1(){
synchronized (i1) {
System.out.println("m1()");
while(true){
}
}
}
void m2(){
synchronized (i2) {
System.out.println("m2()");
while(true){
}
}
}
public static void main(String[] args) {
final Test_14 t = new Test_14();
new Thread(new Runnable() {
@Override
public void run() {
t.m1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
t.m2();
}
}).start();
}
}
复制代码
结果:m1和m2方法用的同一个常量,另一个线程一直处于等待状态
m1()
复制代码
二、volatile关键字
1、volatile的可见性
/**
* volatile关键字
* volatile的可见性
* 通知OS操作系统底层,在CPU计算过程中,都要检查内存中数据的有效性。保证最新的内存数据被使用。
*
*/
package concurrent.t01;
import java.util.concurrent.TimeUnit;
public class Test_09 {
volatile boolean b = true;
void m(){
System.out.println("start");
while(b){}
System.out.println("end");
}
public static void main(String[] args) {
final Test_09 t = new Test_09();
new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.b = false;
}
}
复制代码
结果:方法执行结束
start
end
复制代码
2、volatile的非原子性问题
/**
* volatile关键字
* volatile的非原子性问题
* volatile, 只能保证可见性,不能保证原子性。
* 不是枷锁问题,只是内存数据可见。
*/
package concurrent.t01;
import java.util.ArrayList;
import java.util.List;
public class Test_10 {
volatile int count = 0;
/*synchronized*/ void m(){
for(int i = 0; i < 10000; i++){
count++;
}
}
public static void main(String[] args) {
final Test_10 t = new Test_10();
List<Thread> threads = new ArrayList<>();
for(int i = 0; i < 10; i++){
threads.add(new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}));
}
for(Thread thread : threads){
thread.start();
}
for(Thread thread : threads){
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(t.count);
}
}
复制代码
结果:没有按照我们想象的100000次,少于100000次
37859
复制代码
三、AtomicXxx
/**
* AtomicXxx
* 同步类型
* 原子操作类型。 其中的每个方法都是原子操作。可以保证线程安全。
*/
package concurrent.t01;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Test_11 {
AtomicInteger count = new AtomicInteger(0);
void m(){
for(int i = 0; i < 10000; i++){
/*if(count.get() < 1000)*/
count.incrementAndGet();
}
}
public static void main(String[] args) {
final Test_11 t = new Test_11();
List<Thread> threads = new ArrayList<>();
for(int i = 0; i < 10; i++){
threads.add(new Thread(new Runnable() {
@Override
public void run() {
t.m();
}
}));
}
for(Thread thread : threads){
thread.start();
}
for(Thread thread : threads){
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(t.count.intValue());
}
}
复制代码
结果:
100000
复制代码
四、门闩 - CountDownLatch
/**
* 门闩 - CountDownLatch
* 可以和锁混合使用,或替代锁的功能。
* 在门闩未完全开放之前等待。当门闩完全开放后执行。
* 避免锁的效率低下问题。
*/
package concurrent.t01;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class Test_15 {
CountDownLatch latch = new CountDownLatch(5);
void m1(){
try {
latch.await();// 等待门闩开放。
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m1() method");
}
void m2(){
for(int i = 0; i < 10; i++){
if(latch.getCount() != 0){
System.out.println("latch count : " + latch.getCount());
latch.countDown(); // 减门闩上的锁。
}
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("m2() method : " + i);
}
}
public static void main(String[] args) {
final Test_15 t = new Test_15();
new Thread(new Runnable() {
@Override
public void run() {
t.m1();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
t.m2();
}
}).start();
}
}
复制代码
结果:
latch count : 5
m2() method : 0
latch count : 4
m2() method : 1
latch count : 3
m2() method : 2
latch count : 2
m2() method : 3
latch count : 1
m1() method
m2() method : 4
m2() method : 5
m2() method : 6
m2() method : 7
m2() method : 8
m2() method : 9
复制代码
练习:
自定义容器,提供新增元素(add)和获取元素数量(size)方法。
启动两个线程。线程1向容器中新增10个数据。线程2监听容器元素数量,当容器元素数量为5时,线程2输出信息并终止。
复制代码
1)、使用volatile关键字
/**
* volatile
*/
package concurrent.t02;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Test_01 {
public static void main(String[] args) {
final Test_01_Container t = new Test_01_Container();
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 10; i++){
System.out.println("add Object to Container " + i);
t.add(new Object());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
while(true){
if(t.size() == 5){
System.out.println("size = 5");
break;
}
}
}
}).start();
}
}
class Test_01_Container{
volatile List<Object> container = new ArrayList<>();
public void add(Object o){
this.container.add(o);
}
public int size(){
return this.container.size();
}
}
复制代码
结果:
add Object to Container 0
add Object to Container 1
add Object to Container 2
add Object to Container 3
add Object to Container 4
size = 5
add Object to Container 5
add Object to Container 6
add Object to Container 7
add Object to Container 8
add Object to Container 9
复制代码
2)、wait notify
/**
* wait notify
*/
package concurrent.t02;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Test_02 {
public static void main(String[] args) {
final Test_02_Container t = new Test_02_Container();
final Object lock = new Object();
new Thread(new Runnable(){
@Override
public void run() {
synchronized (lock) {
if(t.size() != 5){
try {
lock.wait(); // 线程进入等待队列。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("size = 5");
lock.notifyAll(); // 唤醒其他等待线程
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for(int i = 0; i < 10; i++){
System.out.println("add Object to Container " + i);
t.add(new Object());
if(t.size() == 5){
lock.notifyAll();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}
class Test_02_Container{
List<Object> container = new ArrayList<>();
public void add(Object o){
this.container.add(o);
}
public int size(){
return this.container.size();
}
}
复制代码
结果:
add Object to Container 0
add Object to Container 1
add Object to Container 2
add Object to Container 3
add Object to Container 4
size = 5
add Object to Container 5
add Object to Container 6
add Object to Container 7
add Object to Container 8
add Object to Container 9
复制代码
3)、CountDownLatch
/**
* CountDownLatch 门闩
*/
package concurrent.t02;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class Test_03 {
public static void main(String[] args) {
final Test_03_Container t = new Test_03_Container();
final CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable(){
@Override
public void run() {
if(t.size() != 5){
try {
latch.await(); // 等待门闩的开放。 不是进入等待队列
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("size = 5");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 10; i++){
System.out.println("add Object to Container " + i);
t.add(new Object());
if(t.size() == 5){
latch.countDown(); // 门闩-1
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
class Test_03_Container{
List<Object> container = new ArrayList<>();
public void add(Object o){
this.container.add(o);
}
public int size(){
return this.container.size();
}
}
复制代码
结果:
add Object to Container 0
add Object to Container 1
add Object to Container 2
add Object to Container 3
add Object to Container 4
size = 5
add Object to Container 5
add Object to Container 6
add Object to Container 7
add Object to Container 8
add Object to Container 9
复制代码