How Java enables multiple threads to run concurrently

Source: Internet
Author: User

With the development of computer technology, programming model is more and more complex and diversified. But the multithreaded programming model is the final model of computer system architecture at present. As CPU frequency continues to rise, the hardware of the X86 architecture has become a bottle, with a CPU frequency of up to 4G in this architecture. In fact, the current 3.6G CPU is nearing its peak.
If it is not possible to fundamentally update the current CPU's schema (which is unlikely for a long time), then the way to continue to improve CPU performance is Hyper-Threading CPU mode. Then, the operating system, the application to maximize the CPU performance, is to change to the multithreaded programming model-based parallel processing system and concurrent applications.
Therefore, mastering the multithreaded programming model is not only the means to improve the application performance at present, but also the core idea of the next generation programming model. The purpose of multithreaded programming is to "make the most of CPU resources", when the processing of a thread does not need to consume CPU and only deal with resources such as I/o,oembios, so that other threads that need to consume CPU resources have the opportunity to gain CPU resources. Fundamentally, this is the ultimate goal of multithreaded programming.
[The first question to be clarified]

As the difference between programs and processes, to master multithreaded programming, the first thing to understand is: the difference between thread objects and threads.
A thread object is an object that can produce threads. For example, in the Java Platform thread object, Runnable object. A thread is a sequence of pointing orders being executed. On the Java platform, it is the relatively independent process that runs from the start () of a thread object in the body of the Run method.
Given the level of the authors, it is not possible to describe their definitions in more precise terms. But the two concepts that have essential differences are appreciated by beginners, and as the introduction and the analysis of the routines increase, they will slowly understand the true meaning they represent.
The world will start from the easy, the world will begin in fine.

Let's start with the simplest "single thread": (1) The quoted description is only a relatively single thread, (2) is based on Java.
Class beginclass{
public static void Main (string[] args) {
for (int i=0;i<100;i++)
System.out.println ("hello,world!");
}
}
If we have successfully compiled the Java file and then typed it on the command line:

Java Beginclass

What's going on here? Every Java programmer, from the first minute that he starts learning Java, comes into contact with the problem, but you know what it's going to be?
The JVM process is started, and in the same JVM process, there is only one process, which is itself. Then in this JVM environment, all programs run as threads. The JVM first produces a main thread that runs the entry point for the specified program. In this program, the main thread starts running from the main method. When the main method finishes, the main thread runs. The JVM process also exits with it.
What we see is a main thread running the main method, so that there is only one process that executes the program logic we call
is a single thread. This is a single-threaded environment provided to us by the JVM, in fact, the JVM has at least a background thread such as garbage collection and other non-Java threads, but these threads are inaccessible to us and we only consider it to be single-threaded.
The main thread is initiated by the JVM itself, where it is not generated from the thread object. In this thread, it runs the main method, which is the sequence of instructions. Understand it, but it has nothing more to study.
[Contact Multi-threading]

Class MyThread extends thread{
public void Run () {
System.out.println ("Thread say:hello,world!");
}
}

public class morethreads{
public static void Main (string[] args) {
New MyThread ();
New MyThread (). Start ();
System.out.println ("Main say:hello,world");
}
}

Executing this program, the first line of the main method produces a thread object, but no thread is started.
The second line of the main method produces a thread object and starts a thread.

Main method The third line, after generating and starting a thread, the main thread itself continues to execute other statements.
Instead of studying the specifics of the thread object, let's recall the two concepts above, thread objects and threads. In Java, a thread object is a generic object subclass produced by the JVM. A thread is a running process that the CPU allocates to this object. What we're talking about is what this thread is doing, not what a thread object is doing, but what the process is doing. If you don't understand, don't worry, but remember that they're not the same thing.
Are you tired? Why don't you go on?

Based on this style to introduce multi-threading, not everyone likes and accept, if you do not like, just don't waste your time, and if you accept, then look at the next section.
Before I go into the thread object of the Java platform, I first insert two basic concepts.

[Concurrency and parallelism of threads]

In a single CPU system, the system is scheduled to run only one thread at a time, although there are many forms of this debug mechanism (mostly time-slice rounds), but in any case, it is called concurrency (concurrent) by constantly switching the threads that need to run to run them. In a multi-CPU system, you can allow more than two threads to run concurrently, which can be called parallel (parallel) in the same way that more than two threads run simultaneously.
In the above include all the discussion later, please friends understand that I can not use the most accurate words to define such as concurrency and parallelism of the term, but I in my experience can be popular to tell you what it is, if you see I said some of the "standard" document is not the same, as long as the meaning of the same, you should not be picky.
[Java Thread Object]

Now let's explore the Java thread object.

In Java, there are two ways to start a thread. The first is to call the start () method of the thread instance directly, and the second is
Pass the runable instance to a thread instance and call its start () method.
As already mentioned, thread objects and threads are two completely different concepts. Here we go deeper, generating an instance of a thread that does not mean that the thread was started. The start thread is to say that the thread of the instance is started on a thread object, and when that thread ends, it does not disappear immediately.
I don't have to say much about the basics you can see from a lot of books. Since it's the basics, I'm also focusing on what I can't read from a normal document. So what I'm going to focus on in this section is the difference between the two threading objects that produce threading methods.
Class MyThread extends thread{
public int x = 0;
public void Run () {
for (int i=0;i<100;i++) {
try{
Thread.Sleep (10);
}catch (Exception e) {}
SYSTEM.OUT.PRINTLN (x + +);
}
}
}

If we generate an instance of Mythread and then call its start () method, then the thread corresponding to this instance is generated:
public class Test {
public static void Main (string[] args) throws exception{
MyThread MT = new MyThread ();
Mt.start ();
}
}

Needless to say, the final print will be 0 to 99, now let's play a little bit of tricks:

public class Test {
public static void Main (string[] args) throws exception{
MyThread MT = new MyThread ();
Mt.start ();
SYSTEM.OUT.PRINTLN (101);
}
}

Needless to say, in the basic article (a) We know that because of the single CPU, the general will print 101, and then print 0 to 99. But we can control the thread and let it run as we mean:
public class Test {
public static void Main (string[] args) throws exception{
MyThread MT = new MyThread ();
Mt.start ();
Mt.join ();
SYSTEM.OUT.PRINTLN (101);
}
}

Well, we finally see that the corresponding thread of Mt instance (if I sometimes say MT thread Please don't blame me, but I try not to say so). After the run is complete, the mainline friend Prints 101. Because we let the current thread (this is the main thread) wait for the MT thread to finish running. "Calling the Join () method on thread object A allows the currently executing threads to wait for thread object A's corresponding thread to run until it finishes running." "Please be sure to understand and memorize this sentence deeply, and I am here to draw this point of knowledge to let you continue to see the following example:
public class Test {
public static void Main (string[] args) throws exception{
MyThread MT = new MyThread ();
Mt.start ();
Mt.join ();
Thread.Sleep (3000);
Mt.start ();
}
}

When the thread object MT is finished, we have the main thread take a break, and then we start the thread again on this thread object. As a result we see:
Exception in thread "main" java.lang.IllegalThreadStateException
That is, when the thread object is run once, it cannot run again for the second time. We can look at it with a concrete implementation:
Public synchronized void Start () {
if (started)
throw new Illegalthreadstateexception ();
started = true;
Group.add (this);
Start0 ();
}

Once an instance of thread is called the start () method, the started token of this instance is marked as true, and the fact that the thread has not executed at all, as long as the call to start () is never run again, which means:
[by Start () of the thread instance, an instance of thread can only produce one thread]
So what should we do if we are going to produce multiple threads on one instance (that is, the thread pool that we often call)? This is the great function that the Runnable interface brings to us.
Class R implements runnable{
private int x = 0;
public void Run () {
for (int i=0;i<100;i++) {
try{
Thread.Sleep (10);
}catch (Exception e) {}
SYSTEM.OUT.PRINTLN (x + +);
}
}
}

Like its name, the instance of Runnable is operational, but it does not run directly, and it needs to be wrapped by the thread object before it can run:
public class Test {
public static void Main (string[] args) throws exception{
New Thread (New R ()). Start ();
}
}

Of course this result is no different from Mt.start (). But if we wrap a runnable instance to the thread object multiple times, we can see that they actually started the thread on the same instance:
public class Test {
public static void Main (string[] args) throws exception{
R r = new R ();
for (int i=0;i<10;i++)
New Thread (R). Start ();
}
}

X is an instance object, but the result is that X is added to 999, indicating that the 10 threads are running on the same R object. Please note that because this example is running on a single CPU, there is no synchronization between multiple threads working on a common object at the same time. This is to illustrate the convenience of simplifying synchronization, and in the real environment you can not predict in what environment the program will run, so be sure to consider synchronization.
Here we make a complete example of the difference in the way the threads are generated in different ways:
Package debug;
Import java.io.*;
Import Java.lang.Thread;
Class MyThread extends thread{
public int x = 0;
public void Run () {
System.out.println (++X);
}
}

Class R implements runnable{
private int x = 0;
public void Run () {
System.out.println (++X);
}
}
public class Test {
public static void Main (string[] args) throws exception{
for (int i=0;i<10;i++) {
Thread t = new MyThread ();
T.start ();
}
Thread.Sleep (10000);//Let the above thread run complete
R r = new R ();
for (int i=0;i<10;i++) {
Thread t = new Thread (r);
T.start ();
}
}
}
The 10 threads generated by the above 10 thread objects are printed 10 times 1 when they are run. The following 10 thread objects produced by 10 threads run with 1 to 10 printed. We refer to the following 10 threads as multiple threads of the same instance (runnable instance).

How Java enables multiple threads to run concurrently

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.