Java Startup threads Three ways

Source: Internet
Author: User

Original: http://blog.csdn.net/typename/article/details/7212512

1. Inherit thread

    1. public class Java_thread extends thread{
    2. public static void Main (String args[])
    3. {
    4. (New Java_thread ()). Run ();
    5. System.out.println ("Main thread Run");
    6. }
    7. Public synchronized void Run ()
    8. {
    9. SYSTEM.OUT.PRINTLN ("Sub thread Run");
    10. }
    11. }

2. Implement the Runnable interface

    1. public class Java_thread implements runnable{
    2. public static void Main (String args[])
    3. {
    4. (New Thread (New Java_thread ())). Start ();
    5. System.out.println ("Main thread Run");
    6. }
    7. public void Run ()
    8. {
    9. SYSTEM.OUT.PRINTLN ("Sub thread Run");
    10. }
    11. }

3. Use directly in the function body

    1. void Java_thread ()
    2. {
    3. Thread t = new Thread (new Runnable () {
    4. public void Run () {
    5. Msoundpoolmap.put (Index, Msoundpool.load (FilePath, index));
    6. Getthis (). Loadmediacomplete ();
    7. }});
    8. T.start ();
    9. }

4. Comparison:

Achieve Runnable Interface Benefits:

1) Suitable for multiple threads of the same program code to process the same resource

2) can avoid the restriction of single inheritance in Java

3) Increase the robustness of the program, code can be shared by multiple threads, code and data Independent.

Inherit the thread class advantage:

1) You can abstract the thread class when you need to use the abstract factory pattern design.

2) Multithreading synchronization

Advantages in function body use

1) reduce the scope by not inheriting the thread or implementing runnable.

Click the current text input QQ mailbox to receive the change data

Historically famous Prime Minister 馮京, his father Feng Shang in the prime, originally had no son. The wife then persuaded him to concubinage. Feng Shang passed the capital bought a concubine, ask the female home where. The woman wept and wept, saying that her father had sold her debts because he owed debts. Feng Shang listened, feeling poor. The woman was immediately returned to her father, and the silver that she paid was also given to the father and daughter. After home, Feng wife asked her husband, how not to buy a concubine back. Feng Shang told his wife. The wife listened and said, "Your kindness merits so well that you don't have to worry about having a son." "Sure enough, after a few months, Feng wife unexpectedly pregnant, and will produce baby that evening, the village neighbors, all of a sudden saw a group of drums, drums, music, escort a champion lang to Feng merchants." That evening, the baby was born, named 馮京. Grow up to obtain the champion, when the number of the Prime minister. From this case, it can be seen that the blessing of God to the residing and virtuous, and to the people who are able to abstain from the evil, is quite remarkable. May the reader also be able to encourage each other the people and abstain from the good fortune.

Shifu wrote "The Romance of the West Chamber", depicting the love of the men's cheating private meeting, which led many people to see the evil thoughts of adultery. The book has not been finished, the author cannot restrain himself, gossiping and die.

Pay attention to this public number to change the heavenly book reply keyword I want to collect the following treasure immediately!

1. Tibet Seven Spiritual Practice information "super full" 2. Children's shaping must-hear course "Children's enlightenment must learn"

3. Taijiquan and decomposition version of "Fitness regimen" 4. Family education "pushy parents are responsible"

to provide a change of life method, change the case, understand the Universe life truth, completely change the fate!

Irregular release to change life practice, tutoring, health, martial arts, road and other valuable resources!

Number: xuefo_com Two-dimensional code long press the picture to identify the QR code attention

Carry forward the traditional culture and build a harmonious society! Rejoice reprint merit, all the peoples of the lucky!

Java Startup threads Three ways

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.