Thread & Java

zhaozj2021-02-16  60

Thread & Java

Now everyone is unfamiliar with the concept of threads, and now the operating system supports multithreading from the bottom layer. Many programs are also added with multithreaded instructions, then why do you introduce multithreaded concepts? Let's simply talk about it.

The process is a concept in front of the thread, the emergence of multi-process, making people feel that PC can do more than one thing at the same time. The multi-process is simple to say that the program is divided into a task. In a certain task, it can run the task when it is waiting for some kind of resource, so that the CPU utilization can be greatly enhanced.

However, there are many shortcomings, such as consuming resources, slow speed, people have thought of a new technology, which is thread, in fact, threads, and processes, to make the program quickly run, run in parallel, and make full use of CPU time.

Why is threading techniques to pay more attention? Because after the graphical interface appears, there must be a sub-task if the constant monitoring button is pressed, if the program can't support parallel, then you can't do anything, in order to solve the problem of parallel, people think of a lot of ways. For example, asynchronous I / O, signal lights, etc. Here we mainly talk about threads. Talking about how the thread is, how to support threads in Java.

1: Threads can improve the application of the application. For example, we have a program to connect to the network. If you get data from the network, if it is the same thread when we get the data, then we can only wait for the data transfer to make other operations, if the data is more The download time is relatively long, can't do it during this time, definitely annoying, but if it is multi-thread, we start another thread to network download data, this time we can do something, such as listening Music, play games. You will definitely like the latter.

2: Multi-thread makes the Utilization of the CPU. Again, we must download the software when downloading the software with the IE 5 downloading software, then why? A very important reason is to support multi-thread in Flashget, IE 5 download software does not support multi-threaded.

3: Take up less system resources. There are many other advantages in multithreading, let's talk so much. Let's take a look at how Java provides support for threads.

Now the operating system supports multi-threading, we can develop our own multi-threaded programs on these operating systems, however, each operational method is very different, if we use the operating system The method provided, then cross-platform is almost impossible, and the advantage of Java language is cross-platform, so Java uses its own mechanism, which is the Java language directly supports multi-threaded. That is to add support for multi-threading in the language itself and virtual machine.

In Java, you can write multithreaded programs as long as you understand the basic concepts of threads.

Java provides an abstract class Thread that has an abstract run () method, when we write the program, as long as inheriting this class, implement the run () method can, in the program, if we want to use another A thread performs some actions, which should write these actions in the run () method, then call the start () method inherited from the Thread class. At this time, JVM is responsible for creating a new thread, in the new In the thread, those code, we can take a look at the following example: public class mythread extends thread {

Mythread Mt = new mythread (); public static void main (string args [])

{

Mt.start ();

For (int i = 0; i <100; i )

{

System.out.println ("i =" i);

}

}

Public void Run ()

{

For (int J = 0; j <100; j )

{

System.out.println ("j =" j);

}

}

}

In this program, when the Mt.Start () is executed, the JVM calls the RUN () method, and the for loop in the main () method is also executed at the same time when the Run () method is executed.

Friends with Java experience will ask, then, when we write those programs with fixed structures, such as writing a mobile phone program must inherit from the MIDlet class, the applet program must inherit from the Applet class, we The program can no longer inherit the Thread class. If you want to multi-thread, you must write a thread class inherited from Thread. Then there must be two classes, is this? Let's tell everyone that Java provides two middle mechanisms to implement multithreading. The previous one is inheritance, the following describes an interface.

Let's take a look at a program first:

IMPORT JAVA.Applet.Applet;

Public Class Mythread Extends Applet Implements Runnable

{Public void init ()

{

Mythread MT = New Ourclass ();

Oc.start ();

Public void Run ()

{

For (int i = 0; i <100; i )

{

System.out.println ("i =" i);

}}}

Everyone should now understand, if you want to use multiple threads in the way, you must implement the RunNable interface when your classes are stated, and the RUN () method in the interface is implemented.

Here, I want to have a certain found friend for Java, I will definitely use the thread. Let's talk about other methods about the thread in Java.

We must write a thread class when writing multithreaded programs in Java. This class has already said that it can be inherited from Thread abstract classes, or it can be implemented in Runnable interface, when we call Start through our thread class () When the method, the system will add a thread to execute the code in the RUN () method, and the code in the Run () method will be destroyed. In addition to running a thread, Java also provides a lot of other methods for threads. The first is the Sleep () method, he means to make a thread sleep a certain time, he has two forms: Sleep (Long Milliseconds) and Sleep (Long MilliseConds, Int Nanoseconds). The previous one is the number of milliseconds specified by a thread sleep, and the latter is the number of milliseconds specified by the specified thread sleep in the specified number of naps. But here should pay attention to the operating system must support milliseconds and nanoseconds. If the operating system refers to the level of milliseconds, then if you use the second method, the system will approximate the nearest milliseconds. Number, then call the first method, if the system does not support millisecond level, then the thread sleep he can identify the most accurate time.

After the start, sleep, it stopped, Java provides a method stop () to stop thread stop, but this method involves some security issues, and now Java is not recommended.

This time the RUN () method to the thread starts in a thread's start () method is to prepare for threads, such as giving thread allocation resources, and more. There is also a period of time after the thread is running until the stop is stopped. Then, in Java, it is considered that the thread is active before the start () method is completed after the start () method is completed. We can determine if a thread is really destroyed through the isalive () method.

The more threads of a system, the more complicated the system, then debugging is quite difficult. Inside the Java system, every thread we generate has a unique name. But the name is meaningless, and it is represented by adding a unique ID after "thread". This is another challenge to the debug person. In order to solve this problem, Java provides a mechanism for a thread named. We can name a thread with a setName (String Name), get a thread name by getname (String Name) method. In addition to these two methods. We can also name a thread by constructor when generating a thread. For threads that are inherited, we use Thread (String Name) to name a thread. If the thread is implemented by implementing the runnable () interface, then we named a thread through Thread (Runnable Target, String Name). This way we face a thread debugging with the exact name when debugging multithreaded programs is more convenient.

Finally, I want to say a little, a thread can't be restarted. It is over, don't think about letting him run again.

Through the above explanation, everyone should understand the concept of Java threads and write some simple Java multi-threaded programs.

There are still many concepts of thread. We will introduce here due to time, space, etc. I hope to have a certain help to my friends.

转载请注明原文地址:https://www.9cbs.com/read-18468.html

New Post(0)