Java multithreaded Programming basics of threading and multithreading

Source: Internet
Author: User
Tags command line thread

[Write in front]

With the development of computer technology, the programming model is more and more complex and diversified. But the multithreaded programming model is the final model of the current computer system architecture. As CPU frequency continues to climb, the hardware of the X86 architecture has become a bottle, in which the CPU frequency of the maximum is 4G. In fact, the current 3.6G CPU is close to the peak.

If you cannot fundamentally update the current CPU's schema (which is unlikely for a long time), then the way to continue improving CPU performance is Hyper-Threading CPU mode. Then, operating systems, applications to maximize CPU performance, is to change to a multithreaded programming model-oriented parallel processing systems 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 "Maximize CPU resources", when the processing of a thread does not need to occupy CPU and only with resources such as I/o,oembios, so that other threads that need to occupy CPU resources have access to CPU resources. Fundamentally, this is the ultimate goal of multithreaded programming.

[The first question to be clarified]

As with 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, thread objects in the Java platform, Runnable objects. Thread, which is a sequence of pointing orders being executed. On the Java platform is the relatively independent process of running the Run method body, starting with the start () of a thread object.

Given the author's level, it is not possible to describe their definition in more precise terms. But these two fundamentally different concepts to learn from the beginner, as the introduction of the in-depth and routine analysis of the increase, it will slowly understand what they represent the true meaning.

It is difficult to begin with easy, and the world will begin with fine things.

Let's start with the simplest "single-threaded": (1) The quoted description is only a relatively single-threaded one, (2) 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 succeeded in compiling the Java file and then typing on the command line:

Java Beginclass

What's happening now? Every Java programmer is exposed to the problem from the first minute he starts learning Java, but you know what it's all about?

The JVM process is started, in the same JVM process, with and only one process, which is itself. Then in this JVM environment, all programs run in threads. The JVM first produces a main thread that runs the entry point of 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.

What we see is a main thread running the main method, so that only one thread executes the process logic of the program we call

It's a single thread. This is a single-threaded environment provided to us by the JVM, and in fact, there are at least a background thread and other non-Java threads at the bottom of the JVM that are garbage collected, but they are not accessible to us, and we only think of it as single-threaded.

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.