Analysis of multithreading in Java

Source: Internet
Author: User
Tags instance method thread class

The source and characteristics of a JAVA language

In this era of high-speed information, businessmen have to the information, products to Internet International Interconnection Web page. Again, behind these unusual pages is a full-featured, safe and reliable programming language, Java is well-deserved. Java is a powerful new programming language developed by Sun Microsystem. is a platform-independent programming language. It is a simple, object-like, distributed, interpreted, keyed, secure, structurally neutral, portable, highly performance, multi-threaded, dynamic, language.

Since the advent of Java, with its simple programming, efficient code, portability is strong, quickly by the vast number of computer programmers of all ages. The Java language, a revolutionary programming language on the Internet with powerful animations, multimedia, and interactivity, has brought the world Web into a new era. The Java language is very similar to C + + and can be used to create secure, portable, multithreaded interactive programs. In addition, the program developed in Java has nothing to do with the platform, and can be run on a variety of platforms. Background development, is an efficient, practical programming method. People can only see the pattern, the results of calculations, etc. in front of the screen. In fact, the operating system often in the background to schedule some events, the flow of management programs. For example, the stack in the operating system, resource allocation and management between threads, memory creation, access, management, etc. . Here are a few more threads to talk about.

Multithreading Theory of two Java

2.1 Introduction

Java provides multithreaded functionality that allows multiple small tasks to be performed simultaneously in a single program. Threads sometimes call a small process a small, independent process that is divided into large processes. Because Java implementation of multithreading technology, so more than C and C + + key strong. The greater benefit of multithreading is better interaction performance and real-time control performance. Of course, real-time control performance also depends on the system itself (Unix,windows,macintosh, etc.), in the development of ease and performance is better than single-threaded. The traditional programming environment is usually single-threaded, because Java is multi-threaded. Although multithreading is a powerful and agile programming tool, it is not easy to use well, and there are many pitfalls, even if the programmers can not avoid misuse. To better understand the threads, use the office staff as a metaphor. Office workers are like CPUs, doing work as instructed by their superiors, like executing a thread. In a single-threaded environment, each program is written and executed in a way that the program considers only one processing order at any time. In our metaphor, it's like office workers are undisturbed and distracted from beginning to end, only doing a job. Of course, in real life, it is difficult for a worker to have one task at a time, and more often a staff member has to do several things at once. The boss gave the job to the staff, hoping that the staff would do the job, do some of that work, and so on. If a task cannot be done, such as when a worker waits for information from another department, the worker puts the work aside and goes to another job. In general, the boss wants the various tasks that the staff have on hand to get some progress every day. This introduces the concept of multithreading. The multithreaded programming environment is very similar to this typical office, with several tasks or threads assigned to the CPU. Like office workers, a computer CPU cannot actually do a few things at the same time, but instead allocates time to different threads, making each thread a little bit more progressive. If a thread is unable to do so, for example, the keyboard input requested by the thread has not yet been obtained, the work is transferred to a different path. Typically, the switch between the CPU threads is very rapid, making people feel as if all of them are at the same time.

Any processing environment, whether single-threaded or multi-threaded, has three key aspects. The first is the CPU, which is actually doing computer activity, the second is the code of the executing program, and the third is the data of the program operation.

In multithreaded programming, each thread uses code to provide the behavior of the thread, using the data supply coding operation. Multiple threads can process the same encoding and data at the same time, and different threads may have different encodings and data. In fact, the coding and data parts are quite independent and can be supplied to the thread when needed. So often several threads use the same encoding and different data. This idea can also be likened to office workers. The accountant may have to do one department's account or several or several departments ' accounts. The task of doing accounts in any case is the same as the program code, but the data for each department is different. Accounting may have to do the entire company's account, there are several tasks, but some data is shared, because the company ledger needs data from various departments.

The multithreaded programming environment uses a convenient model to hide the fact that the CPU is switching between tasks. The model allows you to pretend to have more than one available CPU. In order to establish another task, the programmer asks another virtual CPU to start executing a segment of a program with a data group. Let's build the thread below.

Creating Threads

Creating a thread in Java is not difficult, and the three things you need: The code that executes, the data that the code operates, and the virtual CPU that executes the code. The virtual CPU is wrapped in an instance of the thread class. When you create a thread object, you must provide the code that is executed and the data that is processed by the code. Java's object-oriented model requires that program code can only be written as a member method of a class. Data can exist only as an automatic (or local) variable in a method or as a member of a class. These rules require that the code and data provided for the thread should appear as instances of the class.

Public class SimpleRunnable implemants Runable{
Private String message;
Public static void main(String args[]){
SimpleRunnable r1=new SimpleRunnable(“Hello”);
Thread t1=new Thread(r1);
t1.start();
}
public SimpleRunnable(String message){
this.message=message;
}
public void run(){
for(;;){
System.out.println(message);
}
}
}

When the thread starts executing, it executes in the public void run () method. This method is the starting point for the defined thread execution, as if the application started from main () and the applet starts with Init (). The local data for the thread operation is a member of the object of the incoming thread.

First, the main () method constructs an instance of the Simplerunnable class. Note that the instance has its own data, a string that is initialized to "Hello". Because the instance R1 incoming thread class builder, this is the data processed by the thread runtime. The code executed is instance method run ().

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.