重入锁:
synchronized加到方法上锁的就是当前类的实例。
package com.roocon.thread.t6;
public class Demo {
public synchronized void a () {
System.out.println("a");
// b();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void b() {
System.out.println("b");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Demo d1= new Demo();
Demo d2= new Demo();
new Thread(new Runnable() {
@Override
public void run() {
d1.a();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
d2.b();
}
}).start();
}
}
Demo d1= new Demo();
new Thread(new Runnable() {
@Override
public void run() {
d1.a();
}
}).start();
---实验1就是a方法中调用b方法,可以重入。
Demo d1= new Demo();
new Thread(new Runnable() {
@Override
public void run() {
d1.a();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
d1.b();
}
}).start();
---实验2锁住一个对象,a,b方法只打印,方法调用是会等待的,a方法没结束不可以调用b方法。---
-----t6----demo----------
自旋锁:旋的是cpu的时间片。再wait notify和自定义锁的时候去了解,轻量级锁,一个线程复制对象头的信息,另外一个线程想获取的话要不停的自旋。
扩展,自旋可重入,自旋的公平非公平:https://blog.csdn.net/qq_34337272/article/details/81252853
cas参数问题:https://www.jianshu.com/p/e13a46e866ae
package com.roocon.thread.t6;
import java.util.Random;
/**
* 多个线程执行完毕之后,打印一句话,结束
* @author worker
*
*/
public class Demo2 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程执行...");
try {
Thread.sleep(new Random().nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 线程执行完毕了...");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程执行...");
try {
Thread.sleep(new Random().nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 线程执行完毕了...");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程执行...");
try {
Thread.sleep(new Random().nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 线程执行完毕了...");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程执行...");
try {
Thread.sleep(new Random().nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 线程执行完毕了...");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程执行...");
try {
Thread.sleep(new Random().nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 线程执行完毕了...");
}
}).start();
while(Thread.activeCount() != 1) {
// 自旋
}
System.out.println("所有的线程执行完毕了...");
}
}
while(Thread.activeCount() != 1) {
// 自旋
}
上面的代码就是自旋。demo。
-----t6---demo2------
死锁:
package com.roocon.thread.t6;
public class Demo3 {
private Object obj1 = new Object();
private Object obj2 = new Object();
public void a () {
synchronized (obj1) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj2) {
System.out.println("a");
}
}
}
public void b () {
synchronized (obj2) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj1) {
System.out.println("b");
}
}
}
public static void main(String[] args) {
Demo3 d = new Demo3();
new Thread(new Runnable() {
@Override
public void run() {
d.a();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
d.b();
}
}).start();
}
}
-----t6---demo3------