Write: create two threads and run them alternately with the main thread.

Source: Internet
Author: User

[Html]/* create two threads and run them alternately with the main thread. */class E implements Runnable {public void run () {for (int x = 0; x <10; x ++) {System. out. println (Thread. currentThread (). getName () + "I am:" + x) ;}} public class Threade_15 {public static void main (String [] args) {E ee = new E (); thread t1 = new Thread (ee); Thread t2 = new Thread (ee); t1.start (); t2.start (); for (int x = 0; x <10; x ++) {System. out. println (Thread. currentThread (). getName () + "I am:" + x) ;}} applet principle: Pass the subclass object of the Runnable interface as an actual parameter to the constructor of the Thread class. Why should we pass the subclass object of the Runnable interface to the constructor of Thread. Because the custom run method belongs to a subclass object of the Runnable interface. Therefore, let the thread specify the run method of the specified object. You must specify the object to which the run method belongs. Call the start method of the Thread class to enable the Thread and call the run method of the subclass of the Runnable interface.

Related Article

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.