2013年9月17日星期二

Understanding of java thread Thread

new Thread (new Runnable () {
@ Override
public void run () {
System.out.println ("Runnable" + Thread.currentThread (). getName ());
}
}) {
public void run () {
System.out.println ("thread =" + Thread.currentThread (). getName ());
};
}. start ();
Guess What are the results ? Why ? Finally, I give you the answer. BONUS lot
------ Solution ------------------------------------- -------
undoubtedly execution result is : "thread = ..."
shoes may be seen as part of the source code run method of Thread
public void run () {
if (target! = null) / / target is a Runnable object
target.run ();
}
know that the shoes may think it will be executed in the run method Runnable ! ignore this part of children's shoes is an important issue
is new Thread () {public void run () {}}. start (); the anonymous class inherits from Thread and override the run method
very easy to judge based on the principle polymorphism override the run method will be executed

------ For reference only ---------------------------------- -----
give me points it , a very simple matter, certainly thread = Thread-0 slightly ...
The first is replicated ... thread
followed undefined thread name ... Thread
Finally, display the current thread ... 0

Also , can you help me solve a problem, I tangled here , huh ... http://bbs.csdn.net/topics/390594004
------ For reference only ---------------------------------------
thread = ..
simple multi- state problem ?
------ For reference only -------------------------------------- -


3 floor of a friend , can you help me solve the problem ? http://bbs.csdn.net/topics/390594004
Thank
------ For reference only --------------------------------- ------
print "Thread", because , you override the run method , the source code in the run method is written so
 public void run() {
        if (target != null) {
            target.run();
        }
    }
and one of the target is actually the Thread class constructor methods Runable is, that anonymous classes , when start , it will execute the run method of the Thread , if run not rewrite , then execute target.run ( ) , or rewrite that code is executed . And here although target is not empty, but after rewriting run , the above code will be overwritten and can not perform target.run (), so only print "Thread ..." this . . . Of course, in the run rewritten to add super.run (), two on it can be printed out . . .
------ For reference only -------------------------------------- -


Yes, that is polymorphic , may have a look -source successor thread
------ For reference only --------------- ------------------------




most critical is mainly polymorphism. But it is easy to overlook the polymorphic
------ For reference only -------------------------------- -------

  
3 floor of a friend , can you help me solve the problem ? http://bbs.csdn.net/topics/390594004   
thanks  


has sharply replied you
------ For reference only ---------------------------- -----------

  
  
  
most critical is mainly polymorphism. But it is easy to overlook the polymorphic   adversary . . .

没有评论:

发表评论