One, multi-threaded
Multithreading is a good solution to improve program efficiency and avoid wasting resources, here are some basic knowledge of multithreading, which is an indispensable part of advanced promotion.
1,thread class
class to implement multithreading needs to implement the Runnable interface, we follow the source code, as follows:
Public class Implements Runnable { /**/ privatestaticnative void registernatives (); Static { registernatives (); } ........}
The thread class implements the Runnable interface, and then we look at what is in the Runnable interface, as shown below, oh, the original Run method
PublicInterfaceRunnable {/*** When an object implementing interface <code>Runnable</code> was used * To create a thread, start ing the thread causes the object ' s * <code>run</code> method to being called in that separately executing * Thread. * <p> means that the object that implements the Runnable interface can be used to create a thread, and this thread is run by the Run method * The general contract of the method <CODE>RUN&L T;/code> is the It may * take any action whatsoever. * * @seeJava.lang.thread#run ()*/ Public Abstract voidrun ();}
So looking back, what's this run method of rewriting in the thread class?
/** Note that this annotation means that when a thread thread is constructed with a single Runnable object, the Run method of the Runnbalb object will be called, otherwise this method has no effect * If This thread is constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object ' s <code>run</code> method is called; * Otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see#start () *@see#stop () *@see#Thread (Threadgroup, Runnable, String)*/@Override Public voidrun () {if(Target! =NULL) {target.run (); } }
So, what is this target? Keep tracking.
/**/ private Runnable target;
Well, the above is a background knowledge, the following is the real material bar.
There are two ways to initialize a thread,
1, directly rewrite the thread class's Run method, note that if you pass a Runnable object to the thread's constructor at this point, the Runnable object's Run method has no effect, because the overridden run method does not call the parent class's Run method
2, pass a Runnable object directly to thread, call the object's Run method
Method 1:
Thread TR =NewThread () {@Override Public voidrun () { while(true){ Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Thread Test"); } } }; Tr.start ();View Code
Method 2:
Thread TR2 =NewThread (NewRunnable () {@Override Public voidrun () { while(true){ Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Runnable test"); } } }){}; Tr2.start ();View Code
For Method 1, we have a little validation
What happens when we pass a Runnable object and rewrite the Run method at the same time? Obviously, the overridden run method does not invoke the original target-related operation, which means that the runnable passed in has no effect, and the following verifies:
NewThread (NewRunnable () {@Override Public voidrun () { while(true){ Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("In Runnbale"); }}}) {@Override Public voidrun () { while(true){ Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("In Thread"); }}}.start (); }View Code
The operating result is:
In Threadin threadin threadin threadin threadin Threadin Thread
Two, timer
(original) Java multithreading a traditional multithreading