Runnable and Thread

Source: Internet
Author: User
Tags ticket

1, runnable is an interface, when the implementation of the interface needs to re-use the Run method, in the Run method to implement its own logic.

2, Thread is a class, it actually implements the Runnable method, that is to say, when you get a thread instance through the new thread, you actually rewrite the runnable inside the Run method. When you create a thread instance by implementing Runnable, you need to pass the runnable instance a thread, and then open it through. Start.

3, runnable can thread synchronization, but thread is not synchronized with threads, such as a total of 10 tickets, you can open 3 threads at the same time to sell tickets, if you inherit thread to create a ticket class, instantiate 3 threads will each sell 10 tickets, altogether sold 30 cards, At that time, by implementing the ticket-selling class created by runnable, you only need to instantiate an object, pass an object to three thread, and then turn on three threads, and you will find that three threads will only sell 10 of them.

Runnable and Thread

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.