Hello everyone, last time we said is Java common class file operation example, this time we say the example is Java multithreaded programming. Stop gossiping and return to the truth. Let’s Talk Android!
For those of you who are confused about the concept of processes and threads, check out the C – C – C – C – C – C. I’m going to assume that you all understand the concepts of processes and threads, so let’s take a look at how multithreaded programming is implemented in Java. Java provides the Thread class and the Runnable interface for multithreaded programming. We simply override the run method and start the Thread with the start method of the Thread class.
The following is the pseudo-code of the program, please refer to:
class A extends Thread { //or implements Runnable{
public void run(a) {
//do some thing}}new A().start(); // Start the thread
Copy the code
In the pseudocode above, we can either inherit the Thread class or implement the Runnable interface, both of which can achieve the effect of multithreading. As a rule of thumb, the Runnable interface is used more often, because it compensates for single-inheritance. When implementing multithreading in this way, you also need to combine the interface with the Thread class so that you can use the start() method in the Thread class. So how do you put them all together? It’s as simple as using the Thread constructor. Both interfaces and classes have one thing in common: the run() method. The run() method is the main body of thread execution, and we can write related work to the run() method, which is automatically called when the thread starts. Do not actively call it, or it will not start the thread.
At this time the officer asked: just introduced the content seems to only have a thread, how can start multiple threads? Just need new A().start(); This is the code used to start a thread in the pseudocode. If you want to start several threads, use this code a few times.
All talk and no action is not our style. Next, we will demonstrate Java multithreaded programming through specific code.
public class ThreadEx extends Thread {
public static class TestThread implements Runnable{
private int data ;
public void setData(int data) {
this.data = data;
}
public int getData(a) {
return data;
}
@Override
public void run(a) {
// TODO Auto-generated method stub
for(int i=0; i<5; ++i){
setData(i+1);
System.out.println(Thread.currentThread().getName()+ " Data :"+ getData()); }}}public static void main(String[] args) {
TestThread threadItem = new TestThread();
Thread t1 = new Thread(threadItem); //connect the the class and interface by constructor
Thread t2 = new Thread(threadItem); // create the second thread
t1.start(); // start the first thread
t2.start(); // start the second thread}}Copy the code
See officers, the above code can be seen as the transformation of the pseudo-code, I believe you can understand the meaning of the code with the annotations in the code. The only thing missing from the code is thread.currentthread ().getname (), which gets the name of the currentThread.
The following is the running results of the program, please refer to:
Thread-0 Data :1
Thread-1 Data :1
Thread-0 Data :2
Thread-1 Data :2
Thread-0 Data :3
Thread-1 Data :3
Thread-0 Data :4
Thread-1 Data :4
Thread-0 Data :5
Thread-1 Data :5
Copy the code
This is a desirable result. You can see that two threads (thread0 and thread1) run alternately, and both threads output the same result. Please note that this is just a reference, there are different results on different computers, even on the same computer at different times. Therefore, it is normal for a viewer to run the above code and get a different result from the one shown here. Since this is the Java virtual machine doing automatic thread scheduling in the background, we don’t interfere with thread scheduling.
Everybody see officer, about Java multithreaded programming example let’s introduce here, want to know what examples there are behind, and listen to the next decomposition!