What is multithreaded programming
Multithread programming technology is an important feature of Java language. The meaning of multithreaded programming is to divide a program task into several parallel subtasks. Especially in network programming, you will find that many functions can be executed concurrently. For example, the network transmission speed is slow, user input speed is slow, you can use two separate threads to complete these two functions, without affecting the normal display or other functions.
Multithreading is compared with single-threaded, ordinary windows using a single threaded program structure, its working principle is: the main programs have a message loop, constantly read messages from the message queue to decide what to do next, generally for a function, only after the function is executed, The main program can receive additional messages to execute. For example, the function of a child function is to read a network data, or read a file, only to read the data or file to receive the next message. You can't do anything in the process of executing this child function. But often read network data and waiting for user input has a lot of time in the waiting state, multithreading use this feature to divide the task into multiple concurrent tasks, you can solve this problem.
Thread classes in Java
To learn multithreaded programming in Java, you need to know how to implement a class that supports multithreading. There are two ways to implement multithreaded classes in Java:
1. Extend the Java.lang.Thread class and use it to overwrite the thread class's Run method.
2. Generate the class that implements the Java.lang.Runnable interface and associate other instances with the Java.lang.Thread instance.
The thread class is the primary class that is responsible for providing thread support to other classes, and to use a class that has threading capabilities, it is possible to extend the thread class, such as Printthread.java, by simply deriving a subclass from the thread class in Java.
Here's an example of how you can write your own multithreaded applications using these two methods.
To create a subclass of the Java.lang.Thread class
The most important method of the thread class is the Run method. The Run method is the method that the new thread executes, so when you generate the Java.lang.Thread subclass, you must have a corresponding run method.
//PrintThread.java
public class PrintThread extends Thread//继承Tread类
private int count=0 //定义一个count变量用于统计打印的次数并共享变量
public static void mainString args //main方法开始
PrintThread p=new PrintThread //创建一个线程实例
p.start //执行线程
for{;;} //主线程main方法执行一个循环,for执行一个死循环
count++
System.out.printcount+″:Main\n″ //主线程中打印count +“main”变量的值,并换行
public void run //线程类必须有的run()方法
for{;;}
count++
System.out.printcount+″:Thread\n″
The above program is the way to inherit Java.lang.Tread and overwrite run. When you start a program with a Java Virtual machine, this program will be a thread and call the main method of the program main class. The main method in this program generates a new thread, and the connection prints "thread". After the thread is started, the main thread continues to print "Main".
Compile and execute this program, and immediately press "CTRL + C" key to interrupt the program, you will see the above two threads continue to print: Xxx:main ... Xxx:thread ..... xxx represents the number, which is the value of count above. On the author's machine, at different times, these two threads print the same number of times, first print 20 main (that is, the first to perform 20 main thread) then print out 50 thread, and then print main ...