java--Multithreading

Source: Internet
Author: User

First, multi-threaded writing steps

1. Threading class Inherits Thread

2. Overriding the Run () method in a thread class

3. Create a thread object in the test class and start the thread with the start () method

How to Runnable interfaces

1. Thread class inheritance

2. Create a Thread object: Assuming that the thread class is runnable, use runnable myrunnable = new Myrunnable (), create the object, and then use the thread t = new thread (myrunnable);

3. Start the thread

Note: This method must create an object

The difference between a thread object calling start (), a method, and calling the run () method

1.run () only the main thread one execution path

2.start (): Multiple execution paths, the main thread and child threads are alternately executed in parallel

Methods for anonymous inner classes: Override the Run method directly in the test class

    New Thread () {            @Override            publicvoid  run () {                //  TODO auto-generated method Stubwhile                 (!  Teemo.isdead ()) {                    gareen.attack (Teemo);}}        ;        T1.start ();

Some common methods of threading

Priority is 1~10, 1 lowest, default priority is 5

Synchronization method:

java--Multithreading

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.