--java the clouds and see the daylight. Analysis of multithreading programming concept

Source: Internet
Author: User

When it comes to Java multithreaded programming, most people will think of inheriting thread or implementing runnable programming, new A thread instance, calling the start () method, which is called by the OS. The process is as follows:

public class mythread extends thread {     @Override      public void run ()  {         System.out.println ("MyThread");     }    public static void  main (String[] args)  {        mythread thread  = new mythread ();         thread.start ();         system.out.println ("Run end! ");     }} 

inherit thread;

public class myrunnable implements runnable {     @Override      public void run ()  {         System.out.println ("myrunnable");     }    public static void  main (String[] args)  {        thread thread =  new thread (New myrunnable ());         thread.start ();         system.out.println ("Run end! ");     }} 

achieve runnable;

Here, one problem is that, regardless of whether you inherit the thread or implement runnable, you need to have a thread instance at the end to activate the threads. So, what is this instantiated thread instance? Represents only one thread. If so, what does the object that implements the runnable represent, and how does it differ from thread? Let's take a closer look at the Java thread.

Java Threads

A thread is a child task that runs independently in the process. Generally understood as lightweight processes, multiple threads share a process resource. Because the thread is allocated and dispatched by the OS, it is easy to generate the resource competition problem, so it becomes the difficulty in the whole multithreaded development.

From the understanding of threading concepts, you can see that real threads should have two concepts, (thread) tasks and (thread) scheduling.

Thread Task

Then take a look at the thread code. Whether you inherit the thread or implement runnable, you need to rewrite a run () method, in which the code is the actual method that the thread executes, that is, the thread task. No mission, what's the thread doing? Play Yourself!!! I'm sure you can't.

From the Runnable interface, it has only one declaration--run, that is, you want to assign a task to its implementation class. There are runnable interfaces, on the one hand, because Java is a single inheritance, it is not possible for threads to inherit the thread to inherit other classes, so there must be runnable. Second, from the object-oriented design principle-a single duty, a class is only responsible for a certain aspect of the task can be, to avoid the introduction of multi-variable, multi-variable represents more uncertainty, runaway opportunities will increase. Therefore, it is necessary to runnable, only to assign thread tasks.

thread is also the existence of the run abstract method, which is itself a thread (which temporarily does not say). By it the instance itself then activates itself is also possible, otherwise, only one activation and no task, how can it be justified. In fact, thread is also to achieve runnable, their own tasks also refer to others ...

public static void Main (string[] args) {MyThread thread = new MyThread ();        Thread.Start (); System.out.println ("Run end!    "); }

This mythread inherits from the thread class and (hidden) overrides the Run () method. No mission to play anything!!!

Thread scheduling

If you have a task, someone needs to do it, you need to have a dispatch. Who is it? That was the question that was asked at the outset. Threads are required to have a thread instance in both ways, which is thread scheduling.

For the inherited thread, we understand that we can activate our own task, then for the implementation of the thread, how to activate it?

In thread, there is a thread (Runnable target) construction method, which means that there is a Runnable variable in thread. In this case, the thread is commented: "What would be run." And look at the thread's dispatch code:

@Overridepublic void Run () {if (target! = null) {Target.run (); }}

Target is the runnable instance, which is to see if there is a separate task table (Runnable can be seen as a task table), and if not, look at your tasks.

Well, it's obvious now that the thread class is actually more of a responsibility for threading (although you can also assign tasks), runnable is used to assign thread tasks. In multi-threaded programming, the problem of competition, security and synchronization of resource processing occurs on the thread task. Look at the Sync keyword synchronized and thread A little relationship is not. If you do not specify a lock, the default lock object is the task instance. Related to state, priority, communication and so on, really involves the running of the thread itself, these are the problems of thread dispatch processing.

Threads and shares

to further illustrate thread tasks and thread scheduling, here's a common example of thread variable sharing:

public class mythread extends thread {    private int  Count = 5;    public mythread () {}    /**  set thread name  */    public mythread (String name) {         this.setname (name);    }     @Override      public void run ()  {        while (count>0) {             count -- ;             system.out.println ("by  "  +  This.currentthread (). GetName ()  +  "  Calculate,count:= "  +count);         }    }    public static void  Main (String[] args)  {  &NBSp;     mythread thread = new mythread ();         thread a = new thread (thread,  "a");         thread b = new thread (thread,  "B");         thread c = new thread (thread,  "C");         a.start ();         b.start ();         c.start ();     }}

The results of the operation are as follows (different running results are not the same):

650) this.width=650; "src=" Http://www.ituring.com.cn/download/021o8BMihCFA "alt=" Enter image description here "style= "vertical-align:middle;border:0px;"/>

Safety is not considered here. As you can see, here are three thread schedulers that perform a task together to implement thread variable sharing: The thread scheduler a operation changes the count value, and then the thread scheduler B then changes the count ...

The above is a preliminary understanding of Java Threading Programming Concepts ...


This article is from the "11050012" blog, please be sure to keep this source http://11060012.blog.51cto.com/11050012/1904413

--java the clouds and see the daylight. Analysis of multithreading programming concept

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.