The basics of Java multithreaded programming in real combat

Source: Internet
Author: User
Tags thread

Using multithreading in Java programs is much easier than in C or C + +, because the Java programming language provides language-level support. This article demonstrates how intuitive multithreading in Java programs can be through simple programming examples. After reading this article, users should be able to write simple multithreaded programs.

Why do you wait in line?

Here's a simple Java program that completes four unrelated tasks. Such a program has a single control thread that controls the linear movement between these four tasks. In addition, because the required resources-printers, disks, databases, and displays-have intrinsic latency due to hardware and software limitations, each task contains significant latency. Therefore, the program must wait for the printer to complete the task of printing the file before accessing the database, and so on. If you are waiting for the program to complete, this is a poor use of computing resources and your time. One way to improve this program is to make it multithreaded. Four unrelated tasks

class myclass {
  static public void main(String args[]) {
   print_a_file();
   manipulate_another_file();
   access_database();
   draw_picture_on_screen();
  }
}

In this case, each task must wait for the previous task to complete before starting, even if the task involved is irrelevant. However, in real life, we often use multithreaded models. While we are dealing with certain tasks, we can also allow children, spouses and parents to accomplish other tasks. For example, when I write a letter, I may send my son to the post office to buy stamps. In software terms, this is called multiple control (or execution) threads. There are two different ways to get multiple control threads:

Multiple processes

Multiple processes can be created in most operating systems. When a program starts, it can create a process for each task that is about to begin and allow them to run at the same time. When a program is blocked by waiting for network access or user input, another program can run, which increases resource utilization. However, creating each process in this way pays a price: setting up a process takes up a significant portion of the processor time and memory resources. Also, most operating systems do not allow processes to access the memory space of other processes. As a result, interprocess communication is inconvenient and does not provide itself to an easy programming model.

Thread

Threads are also called lightweight processes (LWP). Because threads can only be active within the scope of a single process, creating a line turndown creates a process that is much cheaper. Thus, threads are preferable to processes because they allow for collaboration and data exchange and are very inexpensive to compute resources. Threads require the support of the operating system, so not all machines provide threads. The Java programming language, as a fairly new language, combines thread support with the language itself, which provides robust support for threads.

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.