2013年12月4日星期三

About a thread synchronization problem .....

Concurrent programming in java to see actual combat, a re-entry problems encountered , said synchronization method is a subclass of the parent class time synchronization method calls , using a lock , I made the following test , the lock is indeed found to subclass but I do not understand why this is so . . . After all, two classes , I always felt different classes , his method locks the object of the current class , here how different classes , the lock is the same ? Please enlighten me master ! !

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ParentWidget
{
public void doSomething ()
{
synchronized (this)
{
System.out.println (" parent lock object" ) ;
System.out.println (this.hashCode ());
}
}
}

class LoggingWidget extends ParentWidget
{
public void doSomething ()
{
synchronized (this)
{
System.out.println (" sub-class lock object" ) ;
System.out.println (this.hashCode ());
super.doSomething ();
}
}
}

class MyThread extends Thread
{
private LoggingWidget lw;

public MyThread (LoggingWidget lw)
{
this.lw = lw;
}

@ Override
public void run ()
{
this.lw.doSomething ();
}
}


public class Widget
{
public static void main (String [] str)
{
LoggingWidget lw = new LoggingWidget ();
ExecutorService pool = Executors.newCachedThreadPool ();
for (int i = 0; i <3; i + +)
{
MyThread tmp = new MyThread (lw);
pool.execute (tmp);
}
}
}

------ Solution ------------------------------------ --------
me to answer this question it , LZ read very carefully , as I did not pay attention to this issue , specifically about the investigation .
First, the idea is this:
compile synchronized time, will form a start-end code block for a reference pointer lock operation , LZ code lock is the object lock, in other words , the object itself is locked, there is no problem , causing ambiguity is a subclass call the parent class method of the parent class method also has a lock, the lock locks and lock subclass is not the same object.

The answer is that the lock is the same object, so this re- interpretation of this concept into the lock is completed without problems. Then , why is the same lock it? Here JAVA processing time to pay attention to the inheritance of memory allocation problem , simply instantiate a subclass of course there are several objects. The answer is one , the memory will be allocated to the sub- class object memory space , which is only a sub-class of an object . Then the father of the field is how to access ? The answer is that the parent class is loaded , but has not been instantiated , JVM memory space allocated for the sub-category , when there will be a block of memory for storing the parent domain , the memory block with a super pointer references. This interpretation is completed , the conclusion : there is only one object , only one lock, lock the threads in reentrant .
------ For reference only -------------------------------------- -
execute this method where
------ For reference only ----------------------------- ----------
from the landlord code point of view, you create an instance of a subclass into subclasses doSomething method there any questions?
------ For reference only -------------------------------------- -
I think the landlord is this a problem with the super understand , I understand not very good, the landlord may be puzzled why the hashCode value in the same ? My understanding is that super is not an instance of a subclass of the parent class away call the parent class 's function , from beginning to end program is actually not an instance of a parent class , this has been the instance of a subclass , so hashCode is a value , say the landlord does not know can understand not
------ For reference only ---------------------------- -----------
to rui888 execute method is thread pond , to lwb314 Thank you to answer , I feel like understanding , but it feels a little problem where , can not understand into , doSomething method of the parent class method is actually a subclass ?
------ For reference only -------------------------------------- -

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Widget {
public static void main(String[] str) {
LoggingWidget lw = new LoggingWidget();
ExecutorService pool = Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
MyThread tmp = new MyThread(lw);
pool.execute(tmp);
}
}
}

class ParentWidget {
public void doSomething() {
synchronized (this.getClass()) {
System.out.println(this.getClass());
System.out.println("父类加锁对象");
System.out.println(this.hashCode());
}
}
}

class LoggingWidget extends ParentWidget {
public void doSomething() {
synchronized (this.getClass()) {
System.out.println(this.getClass());
System.out.println("子类加锁对象");
System.out.println(this.hashCode());
super.doSomething();
}
}
}

class MyThread extends Thread {
private LoggingWidget lw;

public MyThread(LoggingWidget lw) {
this.lw = lw;
}

@Override
public void run() {
this.lw.doSomething();
}
}

------ For reference only ------------------------------- --------
still want to ask the big guy , and I understand this "can not be understood as , doSomething method of the parent class is actually a subclass method ? " correctly ?
------ For reference only ------------------------------- --------
ygycomon, thank you enlighten ! ! ! suddenly see the light !!!!!

没有评论:

发表评论