Home > Java > JavaBase > body text

How to implement java multithreading

angryTom
Release: 2019-11-11 13:55:32
Original
3577 people have browsed it

How to implement java multithreading

How to implement java multi-threading

Java multi-threading is one of the advanced features of Java. Through multi-threading, we It can realize multi-tasking and collaborative work at the same time, and improve program efficiency under certain circumstances. However, Java multi-threading must still be used with caution. First of all, Java multi-threading requires high coding skills. Once used improperly, it will cause program errors and threads. Malicious competition for deadlocks causes the program to freeze. Second, the abuse of multi-threading may cause some key parameters to be disordered. In this case, synchronization and lock management between threads need to be done. Third, thread switching requires additional costs, which is often referred to as "context switching". If used improperly, not only will it not improve efficiency, but it will cause a sharp decrease in efficiency. (Recommended tutorial: java tutorial)

1. Inherit Thread to implement multi-threading

Java provides a super class Thread for us to extend , once you inherit it, you can implement multi-threading by overriding the run method. The specific code is as follows:

import java.io.*;
 
public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
 
    public static void main(String[] args) throws IOException {
        Thread t1 = new MyThread();
        Thread t2 = new MyThread();
        t1.start();
        t2.start();
    }
}
Copy after login

2. By implementing the Runnable interface

Because for some classes, they cannot inherit Thread to implement multi-threading, because Java stipulates that only one super class can be inherited at the same time, but it can implement multiple interfaces at the same time, so Runnable is even more popular. The specific code is as follows:

import java.io.*;
 
public class MyThread implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
 
    public static void main(String[] args) throws IOException {
        Thread t1 = new Thread(new MyThread());
        Thread t2 = new Thread(new MyThread());
        t1.start();
        t2.start();
    }
}
Copy after login

The above method directly defines the class to implement the Runnable method. In fact, it can also be modified into an anonymous inner class method to create a Thread, as follows:

import java.io.*;
 
public class MyThread {
    public static void main(String[] args) throws IOException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getId());
            }
        });
 
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getId());
            }
        });
 
        t1.start();
        t2.start();
    }
}
Copy after login

3. Implement a Thread through Callable

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
 
public class MyThread implements Callable<Integer>{
    public static void main(String[] args){
        MyThread t1 = new MyThread();
        FutureTask<Integer> future = new FutureTask<Integer>(t1);
        new Thread(future,"呵呵哒").start();
    }
    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return null;
    }
}
Copy after login

The above is the detailed content of How to implement java multithreading. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!