题目:

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

Java-JUC(十一):线程8锁的更多相关文章

  1. JAVA语言规范-线程和锁章节之同步、等待和通知

    JAVA语言规范:线程和锁 1 同步 java编程语言提供了线程间通信的多种机制.这些方法中最基本的是同步化,此方法是使用监视器实现的.JAVA中每个对象与一个监视器相关联,一个线程可以加锁和解锁监视 ...

  2. JAVA多线程学习十一-线程锁技术

    前面我们讲到了synchronized:那么这节就来将lock的功效. 一.locks相关类 锁相关的类都在包java.util.concurrent.locks下,有以下类和接口: |---Abst ...

  3. Java - "JUC" ReentrantLock获取锁

    [Java并发编程实战]-----“J.U.C”:ReentrantLock之一简介 ReentrantLock介绍 ReentrantLock是一个可重入的互斥锁,又被称为“独占锁”. 顾名思义,R ...

  4. Java - "JUC" ReentrantLock释放锁

    Java多线程系列--“JUC锁”04之 公平锁(二) 释放公平锁(基于JDK1.7.0_40) 1. unlock() unlock()在ReentrantLock.java中实现的,源码如下: p ...

  5. JUC——线程同步锁(锁处理机制简介)

    锁处理机制简介 juc的开发框架解决的核心问题是并发访问和数据安全操作问题,当进行并发访问的时候如果对于锁的控制不当,就会造成死锁这样的阻塞问题. 为了解决这样的缺陷,juc里面重新针对于锁的概念进行 ...

  6. java高并发系列 - 第12天JUC:ReentrantLock重入锁

    java高并发系列 - 第12天JUC:ReentrantLock重入锁 本篇文章开始将juc中常用的一些类,估计会有十来篇. synchronized的局限性 synchronized是java内置 ...

  7. java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)

    一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...

  8. Java线程:锁

    一.锁的原理 Java中每个对象都有一个内置锁,当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行的代码类的当前实例(this实例)有关的锁.获得一个对象的锁也称为获取锁.锁 ...

  9. Java - “JUC”锁

    [Java并发编程实战]-----“J.U.C”:锁,lock   在java中有两种方法实现锁机制,一种是在前一篇博客中([java7并发编程实战]-----线程同步机制:synchronized) ...

  10. Java 线程与锁

    Synchronization synchronized语法可以获取锁, 当其他线程持有锁的时候该线程想要获取锁将会进入等待状态, 直到没有其他线程持有该锁 显示使用 synchronized (lo ...

随机推荐

  1. 为什么不要在 JavaScript 中使用位操作符?

    如果你的第一门编程语言不是 JavaScript,而是 C++ 或 Java,那么一开始你大概会看不惯 JavaScript 的数字类型.在 JavaScript 中的数字类型是不区分什么 Int,F ...

  2. CentOS 7下使用chkconfig添加的服务无法使用/etc/profile里面的环境变量

    经过分析/etc/profile为入口的,基本是登录后执行的变量,而使用chkconfig添加的服务多变以守护经常运行,没有登录. CentOS 7下使用chkconfig添加的服务无法使用/etc/ ...

  3. External Input Counter and External interrupt

    External Input Counter and External interrupt : count the input signal from the button. So what is t ...

  4. leetcode 题解 || Valid Parentheses 问题

    problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  5. vue首屏加载优化

    库使用情况 vue vue-router axios muse-ui material-icons vue-baidu-map 未优化前 首先我们在正常情况下build 优化 1. 按需加载 当前流行 ...

  6. MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器

    MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→[Preferences],如下图所示: 找到WebLogic的配置,如下图所示:

  7. 在Delphi中操作快捷方式

    快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序.文件和文件夹非常有用.在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头 ...

  8. Image控件Stretch属性

    通过设置Image控件Stretch属性的值可以控制图片的显示形式: 包含的值:None.Fill.Uniform.UniformToFill <Grid x:Name="Layout ...

  9. 【docker】docker的简单状态监控

    命令: docker stats 可以使用占位符,显示想要看的信息: docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{ ...

  10. yii开发第一部分之执行流程

    一 目录文件 |-framework 框架核心库 |--base 底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集,将提供特定功能给整个应用程序),CC ...