The good memory is not as good as the bad pen head 77-the Thread objects of the multi-thread-Thread subclass are different, 77 -- thread
The Thread objects of the Thread subclass are different.
For example:
EasySelfThread thread = new EasySelfThread ();
// Same thread object
Thread t1 = new Thread (thread, "t1 ");
Thread t2 = new Thread (thread, "= t2"); // (3)
Because t1 and t2 are two objects, the threads they start can simultaneously access the run () function.
The Thread objects of the Thread subclass are different java source code.
Package com. thread;
/**
* The Thread object generated by the subclass of Thread is the Thread of different objects
*
* @ Author fan fangming
*/
Public class EasySelfThread implements Runnable {// public synchronized void print () {// (1) public void print () {// (1) for (int I = 0; I <3; I ++) {System. out. println (Thread. currentThread (). getName () + ":" + I); try {Thread. sleep (100);} catch (Exception e) {e. printStackTrace () ;}} public void run () {this. print ();} public static void main (String [] args) {EasySelfThread thread = new EasySelfThread (); // Thread t1 = new Thread (thread, "t1"); Thread t2 = new Thread (thread, "= t2"); // (3) t1.start (); t2.start ();}}
Running result
T1: 0
= T2: 0
= T2: 1
T1: 1
= T2: 2
T1: 2
You can also modify the run method with synchronized, but they are still two objects, not one.