Multi-threaded Multi-core Java multi-thread programming technology analysis

Source: Internet
Author: User

Reading Abstract:After the introduction of Java multithreading technology, almost all application development performance has been greatly improved. This article will explore hyper-Threading Technology and the new multi-core Intel processor technology to analyze how these thread technologies have become a standard part of Java programming.



   I. multithreading technology in the Java environment

Building a threaded application often has an important performance impact on the program. For example, consider a program that reads a large amount of data from the disk and processes the data before writing them to the screen (such as a DVD player ). In a traditional single-threaded Program (most of the client programs used today), only one task is executed at a time, and each of these activities occurs as different stages of a sequence respectively. Data can be processed only when a piece of data with a defined size is read. Therefore, the program logic that can process data is not executed until the disk read operation is complete. This will cause very poor performance problems.

In a multi-threaded program, you can allocate a thread to read data, let another thread process data, and let the third thread send data to the graphics card. These three threads can run in parallel. In this way, data can still be processed while reading data from the disk, thus improving the overall program performance. Many examples can be designed to do two things at the same time to further improve performance. Java Virtual Machine (JVM) is widely used for this reason.

This article will discuss how to create multi-threaded Java code and some best practices for parallel programming. It also introduces some tools and resources that are extremely useful to developers. Due to space limitations, it is impossible to fully discuss these issues. Therefore, I would like to focus on the important points and provide you with relevant reference information.

   Ii. Threaded Java code

All programs use at least one thread. In C/C ++ and Java, this refers to the thread started by calling main. In addition, several steps are required to create a thread: Create a New thread and specify a job for it. Once the work is completed, the thread will be automatically killed by JVM.

Java provides two methods to create threads and specify them to work. The first method is to subclass the Java Thread class (in the java. lang Package), and then use the worker function of the Thread to overload the run () method. The following is an example of this method:

Public class SimpleThread extends Thread {
Public SimpleThread (String str ){
Super (str );
}
Public void run (){
For (int I = 0; I <10; I ++ ){
System. out. println (I + "" + getName ());
Try {
Sleep (long) (Math. random () * 1000 ));
} Catch (InterruptedException e ){}
}
System. out. println ("DONE! "+ GetName ());
}
}

This class subclass Thread and provides its own run () method. The function in the code above runs a loop to print the transmitted string to the screen, and then waits for a random number of times. After 10 cycles, the function prints "DONE! ", Then exit-and it will kill this thread. The main function for creating a thread is as follows:

Public class TwoThreadsDemo {
Public static void main (String [] args ){
New SimpleThread ("Do it! "). Start ();
New SimpleThread ("Definitely not! "). Start ();
}
}

Note that this code is extremely simple: The function starts with a given name (which is the string that the thread will print out) and calls start (). Then, start () will call the run () method. The program results are as follows:

0 Do it!
0 Definitely not!
1 Definitely not!
2 Definitely not!
1 Do it!
2 Do it!
3 Do it!
3 Definitely not!
4 Do it!
4 Definitely not!
5 Do it!
5 Definitely not!
6 Do it!
7 Do it!
6 Definitely not!
8 Do it!
7 Definitely not!
8 Definitely not!
9 Do it!
DONE! Do it!
9 Definitely not!
DONE! Definitely not!

As you can see, the output results of these two threads are combined. In a single-threaded program, all "Do it! "The command will be printed together, followed by the output" Definitely not! ".

Different operations of this program will produce different results. This uncertainty comes from two aspects: There is a random pause in the loop; more importantly, the thread execution time cannot be guaranteed. This is a key principle. JVM will run these processes according to its own schedule (virtual machines generally support running these threads as quickly as possible, but there is no way to ensure when a given thread is running ). Each thread can associate a priority with it to ensure that the key thread is processed by JVM before the secondary thread.

The second way to start a thread is to use a class that implements the Runnable interface, which is also defined in java. lang. This Runnable interface specifies a run () method-then the method becomes the main function of the thread, similar to the previous code.

Currently, the general style of Java programs is to support inherited interfaces. By using interfaces, a class can still be inherited (subclass) in the future-if necessary (for example, if the class is to be used as an applet in the future, this will happen ).

Iii. Thread meaning

The multi-thread technology is used to enhance the performance, while it also increases the complexity of the internal operation of the program. This complexity is mainly caused by interactions between threads. It is important to be familiar with these problems, because as more and more core chips are added to Intel processors, the number of threads to be used will also increase accordingly.

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.