`

面试题:当一个线程进入一个对象的同步方法,其他线程能否访问该对象的其他非同步方法

阅读更多
答案经过测试是可以的,只要不是同步方法,就不要等对象锁,所以不管这个对象是否有其它线程锁定了,在其它线程访问非同步方法都不需要等同步锁的动作
笔者写了个例子,以供参考:
package com.fruitking.test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Student {

private String name = "fruitking";
private String consume = "fruitking is a superman!";
private String from = "China";
private String major = "computer science & technology";

public synchronized String getName(){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.name);
try{
Thread.currentThread().sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.name);
return name;
}

public synchronized String getConsume() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.consume);
try{
Thread.currentThread().sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.consume);
return consume;
}

public String getFrom() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.from);
return from;
}
public String getMajor() {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->"+this.major);
try{
Thread.currentThread().sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"-->>"+this.major);
return major;
}
}

package com.fruitking.test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadA implements Runnable {

private Student student;

public void run() {
String str = student.getFrom() + "  ";
str = str + student.getName() + "  ";
str = str + student.getMajor() + "  ";
str = str + student.getConsume() + "  ";
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"  "+ str);
}

public void setStudent(Student student){
this.student = student;
}
}

package com.fruitking.test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadB implements Runnable {

private Student student;

public void run() {
String str = student.getFrom() + "  ";
str = str + student.getConsume() + "  ";
str = str + student.getMajor() + "  ";
str = str + student.getName() + "  ";
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date(System.currentTimeMillis()))+"-->"+Thread.currentThread().getName()+"  "+ str);
}

public void setStudent(Student student){
this.student = student;
}
}

package com.fruitking.test;

public class TestA {

/**
* @param args
*/
public static void main(String[] args) {
Student student = new Student();
ThreadA threadA = new ThreadA();
threadA.setStudent(student);
Thread thread1 = new Thread(threadA);
ThreadB threadB = new ThreadB();
threadB.setStudent(student);
Thread thread2 = new Thread(threadB);
thread1.start();
thread2.start();
}

}


运行结果:(这个结果不是唯一的,但是其意义是不变的)
2009-10-15 11:32:038-->Thread-1-->China
2009-10-15 11:32:038-->Thread-1-->fruitking is a superman!
2009-10-15 11:32:038-->Thread-0-->China
2009-10-15 11:32:041-->Thread-1-->>fruitking is a superman!
2009-10-15 11:32:041-->Thread-1-->computer science & technology
2009-10-15 11:32:041-->Thread-0-->fruitking
2009-10-15 11:32:042-->Thread-1-->>computer science & technology
2009-10-15 11:32:046-->Thread-0-->>fruitking
2009-10-15 11:32:046-->Thread-0-->computer science & technology
2009-10-15 11:32:046-->Thread-1-->fruitking
2009-10-15 11:32:047-->Thread-0-->>computer science & technology
2009-10-15 11:32:051-->Thread-1-->>fruitking
2009-10-15 11:32:051-->Thread-1  China  fruitking is a superman!  computer science & technology  fruitking 
2009-10-15 11:32:051-->Thread-0-->fruitking is a superman!
2009-10-15 11:32:054-->Thread-0-->>fruitking is a superman!
2009-10-15 11:32:054-->Thread-0  China  fruitking  computer science & technology  fruitking is a superman! 

分享到:
评论

相关推荐

    线程同步面试题深入解析

    - 排他性:一个线程在执行同步方法或同步块时,其他线程无法同时访问同一对象的其他同步方法或同步块。 - 等待/唤醒机制:`wait()`、`notify()`和`notifyAll()`用于线程间的通信,控制线程的执行顺序。 - 锁的...

    线程编程面试题

    - **`synchronized`方法的作用**:如果一个线程已经进入了某个对象的`synchronized`方法,其他线程就不能再进入该对象的其他`synchronized`方法,因为它们共享同一把锁。 #### 65、请说出你所知道的线程同步的方法...

    多线程面试题

    继承Thread类直接覆盖run()方法,而实现Runnable接口则需要创建一个实现了Runnable接口的类,并重写run()方法,然后通过Thread类的构造函数传入Runnable对象来创建线程。 3. **线程状态**:Java中的线程有五种状态...

    NET面试题-多线程编程与线程同步1

    【标题】:“NET面试题-多线程编程与线程同步1” 【描述】:本篇文章主要探讨.NET框架下的多线程编程与线程同步相关的面试问题,包括线程与进程的区别、GUI线程访问控件限制、后台线程与前台线程、锁的使用以及线程...

    史上最强多线程面试44题和答案:线程锁+线程池+线程同步等

    - **可见性**:当多个线程访问同一个共享变量时,其中一个线程对这个变量进行了修改,其他线程能够立即看到修改后的结果。为了实现可见性,通常采用`synchronized`关键字或者显式锁`Lock`来确保修改后的值能被所有...

    java经典多线程面试题

    - 线程互斥(Mutual Exclusion)是指多个线程在同一时刻只有一个能进入临界区(Critical Section),也就是访问共享资源的那段代码。 4. synchronized关键字的用法有哪些? - 在方法上使用,对当前对象实例进行...

    2020面试题总结多线程篇.pdf

    线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出现数据不一致或者数据污染。 三、volatile 的原理和作用 ...

    多线程经典面试题和答案

    这种方式简单直接,但是因为 Java 不支持多重继承,所以如果希望该类还能继承其他类,则不推荐这种方法。 2. **实现 `Runnable` 接口**:通过实现 `Runnable` 接口并重写 `run()` 方法,然后将该 `Runnable` 对象...

    面试中多线程问题

    "面试中多线程问题" 多线程是 Java 编程中一个重要...同步静态方法时会获取该类的“Class”对象,所以当一个线程进入同步的静态方法中时,线程监视器获取类本身的对象锁,其它线程不能进入这个类的任何静态同步方法。

    线程面试题

    - `start()`方法用于启动一个新的线程,并调用该线程的`run()`方法。 - 直接调用`run()`方法会在当前线程中执行线程任务,不会创建新线程。 - 使用`start()`方法启动线程后,`run()`方法会在新的线程中执行。 ##...

    多线程,高并发面试题.pdf

    stop()方法会立即释放该线程所持有的所有锁,这将导致线程安全问题,因为如果线程对象正处于一个不一致的状态,其他线程访问这个对象可能导致程序错误。同时,stop()方法也难以调试,因为它可能释放了关键的锁,但...

    java面试题之多线程

    - 当一个线程进入对象的 `synchronized` 方法后,其他线程无法进入该对象的其他 `synchronized` 方法,因为对象的锁已被占用。 6. **线程同步方法**: - `wait()`, `sleep()`, `notify()`, `notifyAll()` 都是...

    73道Java面试题合集-多线程与进程

    以下是对73道Java面试题合集——多线程与进程相关知识点的详细解释。 1. **进程与线程的概念**: - **进程**:是操作系统资源分配的基本单位,拥有独立的内存空间和系统资源,可以并发执行。 - **线程**:是程序...

    java多线程面试题

    可以使用Thread.UncaughtExceptionHandler接口来处理这种异常情况,该接口允许我们设置一个异常处理器,当线程抛出未捕获异常时,JVM会调用该处理器的uncaughtException()方法来进行处理。 知识点11:线程间共享...

    面试题c++mysql多线程操作系统面试题c++mysql多线程操作系统面试题c++mysql多线程操作系统

    在IT行业中,C++、MySQL和多线程操作系统是至关重要的技术领域,它们在软件开发、数据库管理和系统设计中占据核心地位。以下是对这些主题的详细解析,旨在为面试准备提供深入理解。 首先,C++是一种强类型、静态...

    java面试题_多线程(68题).pdf

    3. **自旋锁**:自旋锁是一种同步机制,当线程试图获取一个已被其他线程持有的锁时,它会在循环中等待,直到锁变为可用。自旋锁不释放CPU,因此持有锁的线程应尽快释放锁,以减少其他线程的等待时间。在多处理器系统...

    java面试题之多线程.pdf

    7. **线程安全**:当多个线程访问共享数据时,确保数据的完整性和一致性。使用`synchronized`、`volatile`、`Atomic`类等机制保证线程安全。 8. **线程池**:通过`ExecutorService`和`ThreadPoolExecutor`管理线程...

    多线程面试题.docx

    - 一个线程访问某对象的 synchronized 方法,其他线程仍可以访问该对象的非 synchronized 代码块。 - 一个线程访问某对象的 synchronized 方法,其他线程对同一对象的其他 synchronized 方法或代码块将被阻塞。 5...

Global site tag (gtag.js) - Google Analytics