Java concurrent Programming: Creating Threads

Source: Internet
Author: User

Java concurrent Programming: Creating a thread Table of Contents
    • 1. Thread
    • 2. Runnable
    • 3. Start () and run ()

There are two main ways to create threads in Java, one is by inheriting the abstract class thread, and one by implementing the Runnable interface. Of course, there are also concurent bags inside the callable and future can be considered a kind of.

1Thread

Let's take a look at how thread is created using thread:

 Public class Threaddemo extends Thread {    @Override     Public void Run() {System.out.println(' current thread name ' is: '+ Thread.CurrentThread(). getName());}     Public Static void Main(String[] args) {         for (int I= 0; I < 10; i++) {            Thread Thread=New Threaddemo(); Thread.Start();}    }}
Current thread name is:thread-0current thread name is:thread-2current thread name is:thread-1current thread name is:th Read-3current thread name is:thread-5current thread name is:thread-4current thread name is:thread-6current thread name Is:thread-7current thread name is:thread-8current thread name is:thread-9

As you can see, we create 10 threads, but the order of execution is indeterminate. You can set the priority, which defaults to 5, the maximum is 10, and the minimum is 1. But even if set to priority, the order is not guaranteed.

 Public Static void Main(String[] args) {     for (int I= 0; I < 10; i++) {        Thread Thread=New Threaddemo(); Thread.setpriority(i + 1); Thread.Start();}}

The priority rises gradually, but the results are the same.

2Runnable

We'll use runnable to create the thread:

 Public class Runnabledemo Implements Runnable {    @Override     Public void Run() {System.out.println("Current runnable thread name is:"+ Thread.CurrentThread(). getName());}     Public Static void Main(String[] args) {         for (int I= 0; I < 10; i++) {            Thread Thread=New Thread(New Runnabledemo()); Thread.Start();}    }}
Current runnable thread name is:thread-0current runnable thread name is:thread-3current runnable thread name is:thread- 2Current runnable thread name is:thread-5current runnable thread name is:thread-1current runnable thread name Is:thread -6current runnable thread name is:thread-7current runnable thread name is:thread-4current runnable thread name Is:threa D-8current runnable Thread Name is:thread-9

As you can see, after implementing the Runnable interface interface but the Runnabledemo class but the instance still cannot run directly, it must pass the instance object to the thread class before it can invoke start () in the Thread object.

3Start () and run ()

Let's take a look at the difference between start () and run ():
When we define a new thread class, the only way to overwrite it is run (). So, can we just call the run () method?
The answer is yes.
Let's replace start () with Run () and try again:

 Public Static void Main(String[] args) {     for (int I= 0; I < 10; i++) {        Thread Thread=New Thread(New Runnabledemo()); Thread.run();}}
Current runnable thread name is:maincurrent runnable thread name is:maincurrent runnable thread name is:maincurrent run Nable thread name is:maincurrent runnable thread name is:maincurrent runnable thread name is:maincurrent runnable Threa D name is:maincurrent runnable thread name is:maincurrent runnable thread name is:maincurrent runnable thread name is: Main

The result is that the main thread is running, that is, there are no new threads to start running. Therefore, when using the run () method call, it is called by the current thread, just like a normal function call, and does not start a new thread and then runs the Run () method in a new thread. Only the start () method is used to invoke the new thread, and then the Run () method runs in the new thread.

date:2017-07-04 21:37

Author:wen YANG

created:2017-07-04 Tue 22:44

Emacs 25.2.1 (ORG mode 8.2.10)

Validate

Java concurrent Programming: Creating Threads

Related Article

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.