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

答案经过测试是可以的,只要不是同步方法,就不要等对象锁,所以不管这个对象是否有其它线程锁定了,在其它线程访问非同步方法都不需要等同步锁的动作
笔者写了个例子,以供参考:
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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值