题目:
判断以下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就是指当前类的实例对象,同一个实例对象通用一个锁,不同实例对象使用不用锁。