Analysis of Java multithread Programming technology in Hyper-threading multi-core

Source: Internet
Author: User
Tags thread thread class

Multi-threaded technology in Java environment

Building threaded applications often has a significant performance impact on the program. For example, consider a program that reads large amounts of data from disk and processes the data (such as a DVD player) before it is written to the screen. On a traditional single-threaded program (most of the client programs used today), only one task executes at a time, each of which takes place as a sequence of different stages. Data processing can only be done when a defined size of data is read. Therefore, the program logic that handles the data is not executed until the disk read operation is complete. This can result in very poor performance issues.

In a multithreaded program, you can allocate a thread to read the data, let another thread process the data, and let the third thread transfer the data to the graphics card. These three threads can run in parallel, so that the data can still be processed while the disk is reading, thereby improving the performance of the overall program. Many of the sample programs can be designed to do two things at once to further improve performance. The Java Virtual Machine (JVM) itself is the reason for the widespread use of multithreading technology.

This article will discuss creating multithreaded Java code and some of the best practices for concurrent programming, as well as some tools and resources that are extremely useful to developers. It is not possible to discuss these issues in a comprehensive way, so I would like to focus on the most important areas and provide you with appropriate reference information.

Second, the threading of Java code

All programs use at least one thread. In C + + and Java, this refers to the thread that was started with a call to main (). The creation of a thread requires several steps: Create a new thread, and then assign it some kind of work. Once the work is done, the thread will automatically be killed by the JVM.

Java provides two methods for creating threads and assigning them to work. The first approach is to subclass the Java thread Class (in the Java.lang package) and then overload the run () method with the thread's work function. Here is an example of this approach:

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 is a subclass of 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 looping 10 times, the function prints "done!" and then exits-and kills the thread by it. Here is the main function to create the thread:

public class TwoThreadsDemo {
 public static void main (String[] args) {
new SimpleThread("Do it!").start();
new SimpleThread("Definitely not!").start();
 }
}

Note that the code is extremely simple: the function begins, given a name (it is the string that the thread will print out) and calls start (). Then, start () calls the run () method. The results of the program 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 of these two threads jiuge together. In a single-threaded program, all the "Do it!" The command will print together, followed by the output "definitely not!".

The different runs of this program will produce different results. This uncertainty stems from two aspects: there is a random pause in the loop, and more importantly, because the thread execution time is not guaranteed. This is a key principle. The JVM will run these processes according to its own schedule (virtual machines generally support running these threads as fast as possible, but there is no guarantee when a given thread should run). Each thread can have a priority associated with it to ensure that the critical thread is handled by the JVM before the secondary thread.

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

The general style of Java programs now is to support inherited interfaces. By using an interface, a class can still inherit (subclass) later-if necessary (for example, this happens if the class is to be used later as an applet).

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.