Java Multithreading (vi)

Source: Internet
Author: User
Tags ticket

Creating a thread Runnable interface using the Runnable interface
    • There is only one run () method

    • The thread class implements the Runable interface

    • Facilitates sharing of resources across multiple threads

    • Java does not support multiple inheritance, if you have inherited a base class, you want to implement the Runnable interface class to generate multi-threaded to implement the Runnable object for the parameters to establish a new thread

Comparison of two methods of threading construction

-Using the Runnable interface

You can separate the CPU, code, and data to form a clear model, and you can inherit from other classes

-Direct inheritance of the thread class

Write simple, direct inheritance, override the Run method, and no longer inherit from other classes

Instance

Operation Result:

Open Thread

Main thread End

Thread3 Ready to sleep: 1750

Thread1 Ready to sleep: 1889

Thread2 ready to sleep: 186

Thread2 Sleep End

Thread3 Sleep End

Thread1 Sleep End

Thread internal data sharing

Multiple threads are created with the same object that implements the Runnable interface as arguments, and multiple threads share the same data in the same object

Operation Result:

Open Thread

End Thread

Thread1 ready to sleep: 2224

Thread3 ready to sleep: 2224

Thread2 ready to sleep: 2224

Thread1 Sleep End

Thread2 Sleep End

Thread3 Sleep End

Description

Because of the 3 new threads created with the same Runnable type object, the three threads share the private member Sleeptime of the object, and three threads sleep 2224 milliseconds in this run.

Thread safety issues:

For some applications, data sharing is unsafe, and just one class in the application is a singleton, so do not write data as member variables, such as HttpServlet.

Case 2: Separate and running threads sometimes need to share some data and take into account each other's state and actions.

    • Simulates three ticket outlets with three threads and sells a total of 20 tickets

    • Use 3 threads to mimic the ticketing behavior of 3 ticket outlets

    • These 3 threads should share 20-ticket data

Operation Result:

Start ticketing

Ticket No. 1th for sale: 20

.....

Ticket No. 3rd for sale: 3

Ticket No. 3rd for sale: 1

Ticket No. 1th for sale: 4

End of ticket No. 1th

End of Ticket No. 3rd

Ticket No. 2nd for sale: 2

End of Ticket No. 2nd

Description

In this example, 3 threads are created and each thread invokes the run () method in the same Selltickets object, accessing the variable in the same object (tickets)

If you are creating a subclass of thread to simulate the ticketing process, and then create 3 threads, each thread will have its own methods and variables, although the methods are the same, but the variables are 20 tickets each, so the result will be to sell 20 tickets, and business requirements do not meet.

Java Multithreading (vi)

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.