Home > Java > javaTutorial > body text

How to implement multithreading in java

(*-*)浩
Release: 2019-12-04 09:16:22
Original
6042 people have browsed it

How to implement multithreading in java

Java multi-threading is one of the advanced features of Java. Through multi-threading, we can achieve multi-tasking and collaborative work at the same time, which can improve program efficiency under certain circumstances. However, Java multi-threading Still use with caution. (Recommended Learning: Java Course )

## This is the first point. Java multi -threading requires higher coding skills. Once it is not used properly, the program error will be caused, and malicious competition between threads will be Deadlock causes the program to freeze.

Secondly, 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 well.

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.

How to implement multi-threading in Java

Inherit Thread to implement multi-threading

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

package com.dingtao.test;
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

Achieved 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:

package com.dingtao.test;
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

Implement a Thread through Callable

The specific code is as follows:

package com.dingtao.test;
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 multithreading in java. 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