Java-JUC(十一):线程8锁
题目:
判断以下8种情况,输出的内容
题目一:一个Number实例对象number,两个非静态同步方法getOne,getTwo,两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { number.getTwo(); } }).start(); } } class Number{ public synchronized void getOne(){ System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
one
two
题目二:一个Number实例对象number,两个非静态同步方法getOne,getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { number.getTwo(); } }).start(); } } class Number{ public synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
one
two
题目三:一个Number实例对象number,两个非静态同步方法getOne,getTwo,给getOne添加Thread.sleep(3000),新增普通方法getThree方法,三个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.getTwo,一个线程调用number.getThree),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { number.getTwo(); } }).start(); new Thread(new Runnable() { public void run() { number.getThree(); } }).start(); } } class Number{ public synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } public void getThree(){ System.out.println("three"); } }
输出结果:
three
one
two
题目四:两个Number实例对象number,number2,两个非静态同步方法getOne,getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number2.getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); final Number number2=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { // number.getTwo(); number2.getTwo(); } }).start(); } } class Number{ public synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
two
one
题目五:一个Number实例对象number,一个静态同步方法getOne,一个非静态同步方法getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number();new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { number.getTwo(); } }).start(); } } class Number{ public static synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
two
one
题目六:一个Number实例对象number,两个静态同步方法getOne,getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); //final Number number2=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { number.getTwo(); // number2.getTwo(); } }).start(); } } class Number{ public static synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public static synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
one
two
题目七:两个Number实例对象number,number2,一个静态同步方法getOne,一个非静态同步方法getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.2getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); final Number number2=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { // number.getTwo(); number2.getTwo(); } }).start(); } } class Number{ public static synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
two
one
题目八:两个Number实例对象number,number2,两个静态同步方法getOne,getTwo,给getOne添加Thread.sleep(3000),两个线程打印输出(一个线程调用number.getOne,另外一个线程调用number.2getTwo),判断输出
package com.dx.juc.test; public class Thread8Monitor { public static void main(String[] args) { final Number number=new Number(); final Number number2=new Number(); new Thread(new Runnable() { public void run() { number.getOne(); } }).start(); new Thread(new Runnable() { public void run() { // number.getTwo(); number2.getTwo(); } }).start(); } } class Number{ public static synchronized void getOne(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("one"); } public static synchronized void getTwo(){ System.out.println("two"); } }
输出结果:
one
two
总结:
1)静态方法锁是锁的Number.class(类的字节码,也是Class的实例);一个类中不管有多少个静态方法,不管访问它们的是多少个线程,这些线程都是公用同一个锁。
2)非静态方法锁是锁的this,也就是当前实例对象。this就是指当前类的实例对象,同一个实例对象通用一个锁,不同实例对象使用不用锁。
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。