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

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.