Java multithreaded--< six > more convenient threading

Source: Internet
Author: User

I. Overview

The title is very abstract, what is more convenient? Compared to who?

Originally, we defined the task to implement the self-runnable or callable interface, but this would necessarily require you to attach the newly defined task to the thread and then invoke the thread to start. Without considering the executor, you will feel that it is very stiff, if it is directly new to a task object, he can start, is not better? This is the origin of the more convenient thread for this title.

Second, the realization

1. Inherit from thread

Because thread implements the Runnable interface, it inherits from the thread class and implements its run method, which is defined as a task.

The benefit of this is that the Start method of the parent class can be called directly to achieve the effect in the overview

 Public classTaskextendsthreadextendsthread{Private intCountdown = 5; Private Static intThreadCount = 0; /** You can call the Run method directly in its construction method*/     PublicTaskextendsthread () {Super(Integer.tostring (+ +threadcount));    Start (); }     PublicString toString () {return"#" +getname () + "(" +countdown+ ")."; }     Public voidrun () { while(true) {System.out.println ( This); if(--countdown = = 0 )                return ; }    }     Public Static voidMain (string[] args) { for(inti = 0; i < 5; i++)            NewTaskextendsthread (); }}

This allows your task to automatically attach to the thread and start when you new an object.

2. Self-managed runnable, implement Runnable interface. such as Selfmanagedthread

Since the implementation of the thread class already has, why should there be self-managed runnable? There is no difference between doing this and inheriting the thread, but doing so will make your class more flexible, implementing the class that can inherit other classes that you want him to inherit.

 Public classSelfmanagedthreadImplementsrunnable{Private intCountdown = 5; PrivateThread T =NewThread ( This); /** Starting a thread in the constructor can become problematic * because another task may start executing before the end of the constructor, which means that the task can access objects in an unstable state*/     PublicSelfmanagedthread () {T.start (); }     PublicString toString () {returnThread.CurrentThread (). GetName () + "(" +countdown+ ")."; } @Override Public voidrun () { while(true) {System.out.println ( This); if(--countdown==0)                return; }    }     Public Static voidMain (string[] args) { for(inti = 0; I < 5; i++)            NewSelfmanagedthread (); }}

Third, and then the egg

This headline is very good, because so far, only know both of these ways, is to start the thread in the constructor, this may become very problematic, because another task may start before the end of the constructor execution, This means that the task may access an object that is in an unstable state. It is preferable to executor a reason for creating a thread object instead of displaying it

But can not say that such a solution is completely useless, to have friends to see the article after giving some hints ...

Iv. Terminology of threads

At this point, the implementation of the thread, most of the summary is finished, the remaining is the competition resources, thread synchronization and other concurrency problems. So, here's a summary of the terms of the thread

Terms
|--thread is not a task, the thread does nothing but drives the task that attaches to it to execute
|--runnable interface Understanding: Can be understood as the inverse encapsulation of the method, that is, it does what it can do
|--actually the task should implement an interface called task, which makes it easier to understand the threads in Java
|--Conceptually: We want to create tasks that run independently of other tasks, so we should be able to define tasks and then say "start" without worrying about the details.
The threading mechanism of |--java is based on a low-level P-threading approach from C, which you have to delve into and need to fully understand all the details of all of its transactions, which are partially infiltrated into the Java implementation, so in order to be at a higher level of abstraction, you must follow the rules when writing code.
|--task: Use the term "task" when describing the work to be performed, and use the "thread" when referencing the specific mechanism that drives the task

Note: The above code is from "Thinking in Java", summary and understanding are from the individual, if there are errors please criticize correct.

Java multithreaded--< six > more convenient threading

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.