引入:继承Thread和实现Runable的区别:
继承Thread:
- package com.openlab.multithreading;
-
- public class TestThread03 extends Thread {
-
- private int count;
-
- @Override
- public void run() {
- for (int i = 0; i < 10000; i++) {
- count++;
- }
- System.out.println(Thread.currentThread().getName()+ ":count = "+ count);
- }
-
- public static void main(String[] args) {
- TestThread03 task1 = new TestThread03();
- TestThread03 task2 = new TestThread03();
- task1.start();
- task2.start();
- }
- }
实现Runable:
使用不同的Thread对象启动同一个Runable线程对象(按照操作系统来说,线程应该共享同一个进程内的内存数据)
- package com.openlab.multithreading;
-
- public class TestThread04 implements Runnable {
-
- private int count;
-
- @Override
- public void run() {// synchronized
- for (int i = 0; i < 10000; i++) {
- count++;
- }
- System.out.println(Thread.currentThread().getName() + ":count = " + count);
- }
-
- public static void main(String[] args) {
- TestThread04 task = new TestThread04();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
注意:如果实现Runable接口中的代码这样写,就和Thread一样了
TestThread04 task = new TestThread04();
TestThread04 task02 = new TestThread04();
new Thread(task).start();
new Thread(task02).start();
继承Thread和实现Runable区别:
继承Thread的线程对象之间不能共享数据。如qq不可能访问到微信上的数据
实现Runable线程对象,如果使用不同的Thread对象启动同一个Runable线程对象,则会共享内 存数据。
这种现象,可能会出现线程安全问题。
线程安全问题:多个线程同时操作同一个共享资源的时候可能会出现业务安全问题
用不同的Thread对象启动同一个Runable线程对象出现各种结果的原因:
++ -- 不具备原子性
例子:
左边加到21,还没赋回去,就被右边抢走了,右边拿到的就是20
右边加到2000,被左边抢走了,根据内存屏障原理,左边不知道值已经变成了2000,左边会把上次的21赋回去。此时,瞬间由2000-->21,少了很多值
如何解决线程安全问题:
通过加锁解决
关键字
java提供了同步锁,synchronized,该锁有三种使用方式
1、同步块
2、将方法加锁
3、将静态方法加锁
该锁必须使用一个对象作为钥匙!!!
java的锁的问题:
synchronized关键字:
是同步锁,关键字
加锁的时候,范围越小越好
synchronized使用方式有三种:
key必须是一个对象
synchronized(key) {
// 同步代码
}
访问到此块时,拿到key的才会进入,其他的会被阻塞,形成一个队列,等待当前拿到钥匙的去释放锁,等到执行完了,释放了,第二个抢到key的线程才会进来
-
- public class TestSynchronized extends Thread {
-
- private int count;
- Object obj = new Object();
-
- @Override
- public void run() {
- for (int i = 0; i < 10000; i++) {
- synchronized (obj) {
- count++;
- }
- }
- System.out.println(Thread.currentThread().getName()+ ":count = "+ count);
- }
-
- public static void main(String[] args) {
- TestSynchronized task = new TestSynchronized();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
第二个线程始终是2000
如果某个方法都是有可能出现线程安全问题
则建议加载方法上面
该方法在充当锁的钥匙!!!(key,java中的函数本质就是对象)
- public class TestSynchronized extends Thread {
-
- private int count;
-
- @Override
- public synchronized void run() {
- for (int i = 0; i < 10000; i++) {
- count++;
- }
- System.out.println(Thread.currentThread().getName()+ ":count = "+ count);
- }
-
- public static void main(String[] args) {
- TestSynchronized task = new TestSynchronized();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
当某个线程进入这个方法,其他线程就不可能进来,只有等这个线程执行完其他才能进来,所以并发度很差,肯定不会出现线程安全问题
当前静态方法所属的静态类(类的字节码对象)在充当锁的钥匙!!!
- public class TestSynchronized extends Thread {
-
- private static int count;
-
- @Override
- public void run() {
- test();
- }
-
- public synchronized static void test() {
- for (int i = 0; i < 10000; i++) {
- count++;
- }
- System.out.println(Thread.currentThread().getName() + ":count = " + count);
- }
-
- public static void main(String[] args) {
- TestSynchronized task = new TestSynchronized();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
因为static拿到的是类的字节码对象,所以我们也可以通过类名.class 拿到当前类的字节码对象
- public class TestSynchronized extends Thread {
-
- private static int count;
-
- @Override
- public void run() {
- for (int i = 0; i < 10000; i++) {
- synchronized (TestSynchronized.class) {
- count++;
- }
- }
- System.out.println(Thread.currentThread().getName() + ":count = " + count);
- }
-
- public static void main(String[] args) {
- TestSynchronized task = new TestSynchronized();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
synchronized同步锁在jdk7之前,默认调用系统锁(重量级锁)
基于如上原因,jdk5,JUC包诞生,提供了一种全新的锁——Lock锁
对象
lock 是一个接口,所以肯定需要利用它的实现子类,这里利用ReentrantLock
lock锁,是jdk5.0提供的锁、是一个可重入锁(ReentrantLock)
可以充当公平锁、也可以是不公平锁。(默认非公平锁,但是构造函数中可以传boolean值进行 修改)
在什么地方出现并发,就在什么地方前一行加锁
一旦加锁,最后必须释放该锁,否则会出现死锁现象。
将释放锁的代码一定要放在finally中!!!
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- public class TestLock extends Thread {
-
- private int count;
- private Lock lock = new ReentrantLock();
-
- @Override
- public void run() {
- for (int i = 0; i < 10000; i++) {
- try {
- // 加锁
- lock.lock();
- count++;
-
- } finally {
- // 一定要记得释放锁!!
- // 建议将释放锁的代码一定要放在finally中!!!
- lock.unlock();
- }
- }
- System.out.println(Thread.currentThread().getName()+ ":count = "+ count);
- }
-
- public static void main(String[] args) {
- TestLock task = new TestLock();
- new Thread(task).start();
- new Thread(task).start();
-
- }
- }
1. synchronized:
jdk1.0提供
关键字,由底层jvm实现的
同步锁
非公平锁
不会产生死锁现象,只要加锁,中途若出现异常,jvm会自动把锁放开
2. Lock:
jdk5.0提供
对象,java代码实现
可重入锁
可以充当公平锁、也可以是非公平锁
一旦加锁,最后必须手动释放该锁,否则会出现死锁现象。
synchronized同步锁在jdk7之前,默认调用系统锁(重量级锁)Oracle提出
synchronized锁有四种状态:无锁,偏向锁、轻量级锁和重量级锁
当一个对象被创建之后,还没有线程进入,这个时候对象处于无锁状态
偏向锁,其实不是真正意义上的锁,仅仅是只有一个线程访问的时候,会将当前线程的编号(pid,也就是线程指针),放在里面,这样标志着这个线程优先权。
相当于无锁状态,如果另一个线程先来,就会被另一个线程抢走,只适合于无并发的情况,效率高。如果出现并发,会进行锁升级,到轻量级锁
在偏向锁的基础上,又有另外一个线程B进来,这时判断对象头中存储的线程A的ID和线程B不一致,就会使用CAS竞争锁,并且升级为轻量级锁
在轻量级锁状态上继续锁竞争,没有抢到锁的线程进行自旋操作,
自旋锁:在一个循环中不停问是否可以获取锁。
当线程没有获得轻量级锁时,线程会CAS自旋来获取锁,当一个线程自旋10次之后(JVM 有一个计数器记录自旋次数,默认允许循环 10 次,可以修改),仍然未获得锁,那么就会升级成为重量级锁。
成为重量级锁之后,线程会进入阻塞队列,线程不再自旋获取锁,而是由CPU进行调度,线程串行执行