Use the runnable interface to implement the thread Mechanism

Source: Internet
Author: User

1) define the real runnable interface of the thread class

Thread mythread = new thread (target) // target is the runnable interface type

The runnable method uses the run () method to implement the thread running content, similar to the implementation in the main method.

Use the runnable interface to provide shared data for multiple threads

The run method of the class implementing the runnable interface can also use the static method of thread.

Public class mythread {public static void main (string [], argS) {runnable1 myrunnable = new runnable (); thread mythread = new thread (myrunnable); mythread. start (); For (INT I = 0; I <100; I ++) system. out. println ("main thread" + I) ;}} class runnable1 implements runnable {public void run () {for (INT I = 0; I <100; I ++) system. out. println ("runnable thread" + I );}}

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.