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