1.doOther方法执行需要等待doSome方法执行结束吗?
main:{
MyClass myClass = new MyClass();
Thread t1 = new MyThread(myClass);
Thread t2 = new MyThread(myClass);
t1.setName("t1");
t2.setName("t2");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
class MyThread extends Thread{
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("t1")){
mc.doSome();
}else{
mc.doOther();
}
}
}
class MyClass{
public synchronized void doSome(){
System.out.println("doSome begin ......");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome end ......");
}
public void doOther(){
System.out.println("doOther begin ......");
System.out.println("doOther end ......");
}
}
答:不需要,doOther没有synchronized修饰,不需要共享对象锁,正常执行。如果doOther方法中加上synchronized,则需要等待doSome方法结束。
2.doOther方法执行需要等待doSome方法执行结束吗?
main:{
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
Thread t1 = new MyThread(myClass1);
Thread t2 = new MyThread(myClass2);
t1.setName("t1");
t2.setName("t2");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
class MyThread extends Thread{
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("t1")){
mc.doSome();
}else{
mc.doOther();
}
}
}
class MyClass{
public synchronized void doSome(){
System.out.println("doSome begin ......");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome end ......");
}
public synchronized void doOther(){
System.out.println("doOther begin ......");
System.out.println("doOther end ......");
}
}
答:不需要,因为MyClass是两个对象,是两个对象锁
3.doOther方法执行需要等待doSome方法执行结束吗?
main:{
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
Thread t1 = new MyThread(myClass1);
Thread t2 = new MyThread(myClass2);
t1.setName("t1");
t2.setName("t2");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
class MyThread extends Thread{
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("t1")){
mc.doSome();
}else{
mc.doOther();
}
}
}
static class MyClass{
public synchronized static void doSome(){
System.out.println("doSome begin ......");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome end ......");
}
public synchronized static void doOther(){
System.out.println("doOther begin ......");
System.out.println("doOther end ......");
}
}
答:需要等,因为synchronized在静态方法中是类锁,不管创建了几个对象,类锁只有一把。
4.死锁
main:{
Object o1 = new Object();
Object o2 = new Object();
Thread t1 = new MyThread1(o1,o2);
Thread t2 = new MyThread2(o1,o2);
t1.start();
t2.start();
}
class MyThread1 extends Thread{
Object o1;
Object o2;
public MyThread1(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public void run() {
synchronized (o1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2){
}
}
}
}
class MyThread2 extends Thread{
Object o1;
Object o2;
public MyThread2(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
@Override
public void run() {
synchronized (o2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1){
}
}
}
}