1.第1段代码
package com.zmh.multithread.juc;
class Phone {
public synchronized void sendSMS() throws Exception {
System.out.println("------sendSMS");
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail");
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendSMS
------sendEmail
原因:如果不加Thread.sleep(100)就不能确定谁先谁后,非静态同步方法的锁是当前对象this,一个类中的所有非静态方法都是同一个锁this,同一时刻只能有一个线程进入同步方法。Thread.sleep()写在哪里就阻塞在哪里,这里写在了main线程里,所以main线程下面的线程BB被阻塞,只能先执行线程AA。
第2段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendSMS
------sendEmail
原因:sleep方法没有释放锁,睡醒了以后接着从以前的上下文的位置继续运行,所以还是先输出------sendSMS。
第3段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
public void hello() {
System.out.println("hello world");
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone.hello();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
hello world
------sendSMS AA
原因:hello方法没有加锁线程可以访问。
第4段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone1 = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
try {
phone1.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone2.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendEmail BB
------sendSMS AA
原因:非静态同步方法的锁是当前对象this,AA线程的锁对象是phone1,BB线程的锁对象是phone2。不是同一个锁,两个线程之间都锁不住对方。
第5段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public static synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public static synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone= new Phone();
new Thread(()->{
try {
phone.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendSMS AA
------sendEmail BB
原因:静态同步方法的锁对象是当前类.Class对象,两个线程都是同一个类的对象(同一个对象)。即同一个类的同一个对象。是同一个锁。
第6段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public static synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public static synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone1= new Phone();
Phone phone2= new Phone();
new Thread(()->{
try {
phone1.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone2.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendSMS AA
------sendEmail BB
原因:静态同步方法的锁对象是当前类.Class对象,两个线程的锁对象都是同一个类的对象(不同对象)。即同一个类的不同对象,但终归属于同一个类,是同一个锁。
第7段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public static synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone= new Phone();
new Thread(()->{
try {
phone.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendEmail BB
------sendSMS AA
原因:静态同步方法的锁对象是当前类.Class对象,非静态同步方法的锁对象是当前对象this,不是同一个锁,相互之间没有影响。好比this是厕所的门,类锁是学校的教室的锁,你锁住了教室,厕所是可以进的,你锁住厕所,教室是可以进的。
第8段代码
package com.zmh.multithread.juc;
import java.util.concurrent.TimeUnit;
class Phone {
public static synchronized void sendSMS() throws Exception {
TimeUnit.SECONDS.sleep(4);
System.out.println("------sendSMS"+"\t"+Thread.currentThread().getName());
}
public synchronized void sendEmail() throws Exception {
System.out.println("------sendEmail"+"\t"+Thread.currentThread().getName());
}
}
/**
*
* @Description: 8锁
* @author gakkimarryme
*
*/
public class Lock_8 {
public static void main(String[] args) throws Exception{
Phone phone1= new Phone();
Phone phone2= new Phone();
new Thread(()->{
try {
phone1.sendSMS();
} catch (Exception e) {
e.printStackTrace();
}
},"AA").start();
Thread.sleep(100);
new Thread(()->{
try {
phone2.sendEmail();
} catch (Exception e) {
e.printStackTrace();
}
},"BB").start();
}
}
输出结果:
------sendEmail BB
------sendSMS AA
原因:同上,也不是同一把锁。